Mercurial > hg > isophonics-drupal-site
comparison core/lib/Drupal/Core/Field/BaseFieldDefinition.php @ 14:1fec387a4317
Update Drupal core to 8.5.2 via Composer
author | Chris Cannam |
---|---|
date | Mon, 23 Apr 2018 09:46:53 +0100 |
parents | 4c8ae668cc8c |
children | c2387f117808 |
comparison
equal
deleted
inserted
replaced
13:5fb285c0d0e3 | 14:1fec387a4317 |
---|---|
57 $field_definition = new static([]); | 57 $field_definition = new static([]); |
58 $field_definition->type = $type; | 58 $field_definition->type = $type; |
59 $field_definition->itemDefinition = FieldItemDataDefinition::create($field_definition); | 59 $field_definition->itemDefinition = FieldItemDataDefinition::create($field_definition); |
60 // Create a definition for the items, and initialize it with the default | 60 // Create a definition for the items, and initialize it with the default |
61 // settings for the field type. | 61 // settings for the field type. |
62 // @todo Cleanup in https://www.drupal.org/node/2116341. | |
63 $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); | 62 $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); |
64 $default_settings = $field_type_manager->getDefaultStorageSettings($type) + $field_type_manager->getDefaultFieldSettings($type); | 63 $default_settings = $field_type_manager->getDefaultStorageSettings($type) + $field_type_manager->getDefaultFieldSettings($type); |
65 $field_definition->itemDefinition->setSettings($default_settings); | 64 $field_definition->itemDefinition->setSettings($default_settings); |
66 return $field_definition; | 65 return $field_definition; |
67 } | 66 } |
597 */ | 596 */ |
598 public function getOptionsProvider($property_name, FieldableEntityInterface $entity) { | 597 public function getOptionsProvider($property_name, FieldableEntityInterface $entity) { |
599 // If the field item class implements the interface, create an orphaned | 598 // If the field item class implements the interface, create an orphaned |
600 // runtime item object, so that it can be used as the options provider | 599 // runtime item object, so that it can be used as the options provider |
601 // without modifying the entity being worked on. | 600 // without modifying the entity being worked on. |
602 if (is_subclass_of($this->getFieldItemClass(), OptionsProviderInterface::class)) { | 601 if (is_subclass_of($this->getItemDefinition()->getClass(), OptionsProviderInterface::class)) { |
603 $items = $entity->get($this->getName()); | 602 $items = $entity->get($this->getName()); |
604 return \Drupal::service('plugin.manager.field.field_type')->createFieldItem($items, 0); | 603 return \Drupal::service('plugin.manager.field.field_type')->createFieldItem($items, 0); |
605 } | 604 } |
606 // @todo: Allow setting custom options provider, see | 605 // @todo: Allow setting custom options provider, see |
607 // https://www.drupal.org/node/2002138. | 606 // https://www.drupal.org/node/2002138. |
622 /** | 621 /** |
623 * {@inheritdoc} | 622 * {@inheritdoc} |
624 */ | 623 */ |
625 public function getPropertyDefinitions() { | 624 public function getPropertyDefinitions() { |
626 if (!isset($this->propertyDefinitions)) { | 625 if (!isset($this->propertyDefinitions)) { |
627 $class = $this->getFieldItemClass(); | 626 $class = $this->getItemDefinition()->getClass(); |
628 $this->propertyDefinitions = $class::propertyDefinitions($this); | 627 $this->propertyDefinitions = $class::propertyDefinitions($this); |
629 } | 628 } |
630 return $this->propertyDefinitions; | 629 return $this->propertyDefinitions; |
631 } | 630 } |
632 | 631 |
639 | 638 |
640 /** | 639 /** |
641 * {@inheritdoc} | 640 * {@inheritdoc} |
642 */ | 641 */ |
643 public function getMainPropertyName() { | 642 public function getMainPropertyName() { |
644 $class = $this->getFieldItemClass(); | 643 $class = $this->getItemDefinition()->getClass(); |
645 return $class::mainPropertyName(); | 644 return $class::mainPropertyName(); |
646 } | 645 } |
647 | 646 |
648 /** | 647 /** |
649 * Helper to retrieve the field item class. | 648 * Helper to retrieve the field item class. |
650 * | 649 * |
651 * @todo: Remove once getClass() adds in defaults. See | 650 * @deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use |
652 * https://www.drupal.org/node/2116341. | 651 * \Drupal\Core\TypedData\ListDataDefinition::getClass() instead. |
653 */ | 652 */ |
654 protected function getFieldItemClass() { | 653 protected function getFieldItemClass() { |
654 @trigger_error('BaseFieldDefinition::getFieldItemClass() is deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Instead, you should use \Drupal\Core\TypedData\ListDataDefinition::getClass(). See https://www.drupal.org/node/2933964.', E_USER_DEPRECATED); | |
655 if ($class = $this->getItemDefinition()->getClass()) { | 655 if ($class = $this->getItemDefinition()->getClass()) { |
656 return $class; | 656 return $class; |
657 } | 657 } |
658 else { | 658 else { |
659 $type_definition = \Drupal::typedDataManager() | 659 $type_definition = \Drupal::typedDataManager() |
802 } | 802 } |
803 | 803 |
804 /** | 804 /** |
805 * {@inheritdoc} | 805 * {@inheritdoc} |
806 */ | 806 */ |
807 public function getUniqueIdentifier() { | |
808 // If we have a specified target bundle, we're dealing with a bundle base | |
809 // field definition, so we need to include it in the unique identifier. | |
810 if ($this->getTargetBundle()) { | |
811 return $this->getTargetEntityTypeId() . '-' . $this->getTargetBundle() . '-' . $this->getName(); | |
812 } | |
813 | |
814 return $this->getUniqueStorageIdentifier(); | |
815 } | |
816 | |
817 /** | |
818 * {@inheritdoc} | |
819 */ | |
820 public function isDeleted() { | |
821 return !empty($this->definition['deleted']); | |
822 } | |
823 | |
824 /** | |
825 * Sets whether the field storage is deleted. | |
826 * | |
827 * @param bool $deleted | |
828 * Whether the field storage is deleted. | |
829 * | |
830 * @return $this | |
831 */ | |
832 public function setDeleted($deleted) { | |
833 $this->definition['deleted'] = $deleted; | |
834 return $this; | |
835 } | |
836 | |
837 /** | |
838 * {@inheritdoc} | |
839 */ | |
807 public function getConfig($bundle) { | 840 public function getConfig($bundle) { |
808 $override = BaseFieldOverride::loadByName($this->getTargetEntityTypeId(), $bundle, $this->getName()); | 841 $override = BaseFieldOverride::loadByName($this->getTargetEntityTypeId(), $bundle, $this->getName()); |
809 if ($override) { | 842 if ($override) { |
810 return $override; | 843 return $override; |
811 } | 844 } |
836 public function setStorageRequired($required) { | 869 public function setStorageRequired($required) { |
837 $this->definition['storage_required'] = $required; | 870 $this->definition['storage_required'] = $required; |
838 return $this; | 871 return $this; |
839 } | 872 } |
840 | 873 |
874 /** | |
875 * Magic method: Implements a deep clone. | |
876 */ | |
877 public function __clone() { | |
878 parent::__clone(); | |
879 | |
880 // The itemDefinition (\Drupal\Core\Field\TypedData\FieldItemDataDefinition) | |
881 // has a property fieldDefinition, which is a recursive reference to the | |
882 // parent BaseFieldDefinition, therefore the reference to the old object has | |
883 // to be overwritten with a reference to the cloned one. | |
884 $this->itemDefinition->setFieldDefinition($this); | |
885 // Reset the static cache of the field property definitions in order to | |
886 // ensure that the clone will reference different field property definitions | |
887 // objects. | |
888 $this->propertyDefinitions = NULL; | |
889 } | |
890 | |
891 /** | |
892 * {@inheritdoc} | |
893 */ | |
894 public function isInternal() { | |
895 // All fields are not internal unless explicitly set. | |
896 return !empty($this->definition['internal']); | |
897 } | |
898 | |
841 } | 899 } |