FieldItemDataDefinition.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Drupal\Core\Field\TypedData;
  3. use Drupal\Core\Field\BaseFieldDefinition;
  4. use Drupal\Core\TypedData\DataDefinition;
  5. /**
  6. * A typed data definition class for defining field items.
  7. *
  8. * This class is just a small wrapper around field definitions to expose
  9. * metadata about field item's via the Typed Data API. As the work is done
  10. * by the field definitions, this class does not benefit and thus does not
  11. * extend from MapDefinition or ComplexDataDefinitionBase.
  12. */
  13. class FieldItemDataDefinition extends DataDefinition implements FieldItemDataDefinitionInterface {
  14. /**
  15. * The field definition the item definition belongs to.
  16. *
  17. * @var \Drupal\Core\Field\FieldDefinitionInterface
  18. */
  19. protected $fieldDefinition;
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function createFromDataType($data_type) {
  24. // The data type of a field item is in the form of "field_item:$field_type".
  25. $parts = explode(':', $data_type, 2);
  26. if ($parts[0] != 'field_item') {
  27. throw new \InvalidArgumentException('Data type must be in the form of "field_item:FIELD_TYPE".');
  28. }
  29. $field_definition = BaseFieldDefinition::create($parts[1]);
  30. return $field_definition->getItemDefinition();
  31. }
  32. /**
  33. * Creates a new field item definition.
  34. *
  35. * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
  36. * The field definition the item definition belongs to.
  37. *
  38. * @return static
  39. */
  40. public static function create($field_definition) {
  41. $definition['type'] = 'field_item:' . $field_definition->getType();
  42. $item_definition = new static($definition);
  43. $item_definition->fieldDefinition = $field_definition;
  44. return $item_definition;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function getPropertyDefinition($name) {
  50. return $this->fieldDefinition->getFieldStorageDefinition()->getPropertyDefinition($name);
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function getPropertyDefinitions() {
  56. return $this->fieldDefinition->getFieldStorageDefinition()->getPropertyDefinitions();
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function getMainPropertyName() {
  62. return $this->fieldDefinition->getFieldStorageDefinition()->getMainPropertyName();
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function getFieldDefinition() {
  68. return $this->fieldDefinition;
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function setFieldDefinition($field_definition) {
  74. $this->fieldDefinition = $field_definition;
  75. return $this;
  76. }
  77. }