->setLabel($definition->getLabel())
->setName($definition->getName())
->setProvider($definition->getProvider())
- ->setQueryable($definition->isQueryable())
->setRevisionable($definition->isRevisionable())
->setSettings($definition->getSettings())
->setTargetEntityTypeId($definition->getTargetEntityTypeId())
* {@inheritdoc}
*/
public function isQueryable() {
- return isset($this->definition['queryable']) ? $this->definition['queryable'] : !$this->isComputed();
+ @trigger_error('BaseFieldDefinition::isQueryable() is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, you should use \Drupal\Core\Field\BaseFieldDefinition::hasCustomStorage(). See https://www.drupal.org/node/2856563.', E_USER_DEPRECATED);
+ return !$this->hasCustomStorage();
}
/**
*
* @return static
* The object itself for chaining.
+ *
+ * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Use
+ * \Drupal\Core\Field\BaseFieldDefinition::setCustomStorage() instead.
+ *
+ * @see https://www.drupal.org/node/2856563
*/
public function setQueryable($queryable) {
+ @trigger_error('BaseFieldDefinition::setQueryable() is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, you should use \Drupal\Core\Field\BaseFieldDefinition::setCustomStorage(). See https://www.drupal.org/node/2856563.', E_USER_DEPRECATED);
$this->definition['queryable'] = $queryable;
return $this;
}
return $this;
}
+ /**
+ * Returns the initial value for the field.
+ *
+ * @return array
+ * The initial value for the field, as a numerically indexed array of items,
+ * each item being a property/value array (array() for no default value).
+ */
+ public function getInitialValue() {
+ $value = isset($this->definition['initial_value']) ? $this->definition['initial_value'] : [];
+
+ // Normalize into the "array keyed by delta" format.
+ if (isset($value) && !is_array($value)) {
+ $value = [
+ [$this->getMainPropertyName() => $value],
+ ];
+ }
+
+ return $value;
+ }
+
+ /**
+ * Sets an initial value for the field.
+ *
+ * @param mixed $value
+ * The initial value for the field. This can be either:
+ * - a literal, in which case it will be assigned to the first property of
+ * the first item;
+ * - a numerically indexed array of items, each item being a property/value
+ * array;
+ * - a non-numerically indexed array, in which case the array is assumed to
+ * be a property/value array and used as the first item;
+ * - an empty array for no initial value.
+ *
+ * @return $this
+ */
+ public function setInitialValue($value) {
+ // @todo Implement initial value support for multi-value fields in
+ // https://www.drupal.org/node/2883851.
+ if ($this->isMultiple()) {
+ throw new FieldException('Multi-value fields can not have an initial value.');
+ }
+
+ if ($value === NULL) {
+ $value = [];
+ }
+ // Unless the value is an empty array, we may need to transform it.
+ if (!is_array($value) || !empty($value)) {
+ if (!is_array($value)) {
+ $value = [[$this->getMainPropertyName() => $value]];
+ }
+ elseif (is_array($value) && !is_numeric(array_keys($value)[0])) {
+ $value = [0 => $value];
+ }
+ }
+ $this->definition['initial_value'] = $value;
+
+ return $this;
+ }
+
+ /**
+ * Returns the name of the field that will be used for getting initial values.
+ *
+ * @return string|null
+ * The field name.
+ */
+ public function getInitialValueFromField() {
+ return isset($this->definition['initial_value_from_field']) ? $this->definition['initial_value_from_field'] : NULL;
+ }
+
+ /**
+ * Sets a field that will be used for getting initial values.
+ *
+ * @param string $field_name
+ * The name of the field that will be used for getting initial values.
+ *
+ * @return $this
+ */
+ public function setInitialValueFromField($field_name) {
+ $this->definition['initial_value_from_field'] = $field_name;
+
+ return $this;
+ }
+
/**
* {@inheritdoc}
*/
public function __sleep() {
// Do not serialize the statically cached property definitions.
$vars = get_object_vars($this);
- unset($vars['propertyDefinitions']);
+ unset($vars['propertyDefinitions'], $vars['typedDataManager']);
return array_keys($vars);
}