ListDataDefinition.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace Drupal\Core\TypedData;
  3. /**
  4. * A typed data definition class for defining lists.
  5. */
  6. class ListDataDefinition extends DataDefinition implements ListDataDefinitionInterface {
  7. /**
  8. * The data definition of a list item.
  9. *
  10. * @var \Drupal\Core\TypedData\DataDefinitionInterface
  11. */
  12. protected $itemDefinition;
  13. /**
  14. * Creates a new list definition.
  15. *
  16. * @param string $item_type
  17. * The data type of the list items; e.g., 'string', 'integer' or 'any'.
  18. *
  19. * @return \Drupal\Core\TypedData\ListDataDefinition
  20. * A new List Data Definition object.
  21. */
  22. public static function create($item_type) {
  23. return static::createFromItemType($item_type);
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function createFromDataType($type) {
  29. $definition = parent::createFromDataType($type);
  30. // If nothing else given, default to a list of 'any' items.
  31. $definition->itemDefinition = DataDefinition::create('any');
  32. return $definition;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public static function createFromItemType($item_type) {
  38. return new static([], \Drupal::typedDataManager()->createDataDefinition($item_type));
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function __construct(array $values = [], DataDefinitionInterface $item_definition = NULL) {
  44. $this->definition = $values;
  45. $this->itemDefinition = $item_definition;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function getDataType() {
  51. return 'list';
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function setDataType($type) {
  57. if ($type != 'list') {
  58. throw new \LogicException('Lists must always be of data type "list".');
  59. }
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function getClass() {
  65. if (!empty($this->definition['class'])) {
  66. return $this->definition['class'];
  67. }
  68. // If a list definition is used but no class has been specified, derive the
  69. // default list class from the item type.
  70. $item_type_definition = \Drupal::typedDataManager()
  71. ->getDefinition($this->getItemDefinition()->getDataType());
  72. if (!$item_type_definition) {
  73. throw new \LogicException("An invalid data type '{$this->getItemDefinition()->getDataType()}' has been specified for list items");
  74. }
  75. return $item_type_definition['list_class'];
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getItemDefinition() {
  81. return $this->itemDefinition;
  82. }
  83. /**
  84. * Sets the item definition.
  85. *
  86. * @param \Drupal\Core\TypedData\DataDefinition $definition
  87. * A list item's data definition.
  88. *
  89. * @return $this
  90. */
  91. public function setItemDefinition(DataDefinitionInterface $definition) {
  92. $this->itemDefinition = $definition;
  93. return $this;
  94. }
  95. /**
  96. * Magic method: Implements a deep clone.
  97. */
  98. public function __clone() {
  99. // Ensure the itemDefinition property is actually cloned by overwriting the
  100. // original reference.
  101. $this->itemDefinition = clone $this->itemDefinition;
  102. }
  103. }