SamplesItem.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Drupal\materio_samples\Plugin\Field\FieldType;
  3. use Drupal\Core\Field\FieldItemBase;
  4. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  5. use Drupal\Core\Form\FormStateInterface;
  6. use Drupal\Core\TypedData\DataDefinition;
  7. use Drupal\Core\TypedData\DataReferenceTargetDefinition;
  8. use Drupal\Core\StringTranslation\TranslatableMarkup;
  9. /**
  10. * Plugin implementation of the 'field_example_rgb' field type.
  11. *
  12. * @FieldType(
  13. * id = "materio_samples_field",
  14. * label = @Translation("Samples"),
  15. * module = "materio_samples",
  16. * category = "Materio",
  17. * description = @Translation("Provide a sample field, each showroom (taxonomy term) can fill it's own sample reference, and only it's own."),
  18. * default_widget = "materio_samples_default_widget",
  19. * default_formatter = "materio_samples_default_formatter"
  20. * )
  21. */
  22. class SamplesItem extends FieldItemBase {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function schema(FieldStorageDefinitionInterface $field_definition) {
  27. return [
  28. 'columns' => [
  29. 'location' => [
  30. 'description' => "The actual location reference in the showroom.",
  31. 'type' => 'text',
  32. 'size' => 'tiny',
  33. // 'not null' => FALSE,
  34. ],
  35. 'target_id' => [
  36. 'description' => 'the id of the target taxonomy term.',
  37. 'type' => 'int',
  38. 'unsigned' => TRUE
  39. ]
  40. ],
  41. 'indexes' => [
  42. 'target_id' => ['target_id'],
  43. ],
  44. ];
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function isEmpty() {
  50. $value = $this->get('location')->getValue();
  51. return $value === NULL || $value === '';
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
  57. $properties['location'] = DataDefinition::create('string')
  58. ->setLabel(t('Reference'));
  59. $properties['target_id'] = DataReferenceTargetDefinition::create('integer')
  60. // ->setLabel(new TranslatableMarkup('@label ID', ['@label' => $target_type_info->getLabel()]))
  61. ->setLabel(new TranslatableMarkup('@label ID', ['@label' => 'Showroom'])) // get voc name from settings
  62. ->setSetting('unsigned', TRUE)
  63. ->setRequired(TRUE);
  64. return $properties;
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public static function defaultFieldSettings() {
  70. return [
  71. // Declare a single setting, 'size', with a default
  72. // value of 'large'
  73. 'vid' => null,
  74. ] + parent::defaultFieldSettings();
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
  80. // dsm($form);
  81. // get vocabularies
  82. $vocabularies = \Drupal\taxonomy\Entity\Vocabulary::loadMultiple();
  83. // dsm($vocabularies);
  84. $options = [null => "choose"];
  85. foreach ($vocabularies as $vid => $voc) {
  86. $options[$voc->id()] = $voc->label();
  87. }
  88. // dsm($options);
  89. $element = [];
  90. // The key of the element should be the setting name
  91. $element['vid'] = [
  92. '#title' => $this->t('Vocabulary'),
  93. '#type' => 'select',
  94. '#options' => $options,
  95. '#default_value' => $this->getSetting('vid'),
  96. ];
  97. return $element;
  98. }
  99. /**
  100. * Form element validation handler; Invokes selection plugin's validation.
  101. *
  102. * @param array $form
  103. * The form where the settings form is being included in.
  104. * @param \Drupal\Core\Form\FormStateInterface $form_state
  105. * The form state of the (entire) configuration form.
  106. */
  107. public static function fieldSettingsFormValidate(array $form, FormStateInterface $form_state) {
  108. $field = $form_state->getFormObject()->getEntity();
  109. dsm($field);
  110. }
  111. }