SamplesDefaultWidget.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Drupal\materio_samples\Plugin\Field\FieldWidget;
  3. use Drupal\Core\Field\FieldItemListInterface;
  4. use Drupal\Core\Field\WidgetBase;
  5. use Drupal\Core\Form\FormStateInterface;
  6. use Drupal\taxonomy\Entity\Term;
  7. use Drupal\workflow\Entity\WorkflowManager;
  8. /**
  9. * Plugin implementation of the 'materio_samples_default_widget' widget.
  10. *
  11. * @FieldWidget(
  12. * id = "materio_samples_default_widget",
  13. * module = "materio_samples",
  14. * label = @Translation("Samples"),
  15. * field_types = {
  16. * "materio_samples_field"
  17. * }
  18. * )
  19. */
  20. class SamplesDefaultWidget extends WidgetBase {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function form(FieldItemListInterface $items, array &$form, FormStateInterface $form_state, $get_delta = NULL) {
  25. // construct "manually" the list of items
  26. $vid = $this->fieldDefinition->getSetting('vid');
  27. $query = \Drupal::entityQuery('taxonomy_term')
  28. ->sort('weight', 'DESC')
  29. // ->sort('tid', 'DESC')
  30. ->condition('vid', $vid);
  31. $tids = $query->execute();
  32. $terms = Term::loadMultiple($tids);
  33. // define the cardinality, this will remove the add_more btn
  34. $this->fieldDefinition->getFieldStorageDefinition()->setCardinality(count($terms));
  35. $locations = [];
  36. foreach ($items as $delta => $item) {
  37. $locations[$item->target_id] = $item->location;
  38. }
  39. $delta = 0;
  40. foreach ($terms as $term) {
  41. // remove masqué
  42. $sid = WorkflowManager::getCurrentStateId($term, 'field_workflow');
  43. if($sid == 'workflow_hidden') continue;
  44. $location = isset($locations[$term->id()]) ? $locations[$term->id()] : '';
  45. $value = array(
  46. 'location' => $location,
  47. 'target_id'=> $term->id()
  48. );
  49. $items->set($delta, $value);
  50. $delta ++;
  51. }
  52. // then call the normal form
  53. $elements = parent::form($items, $form, $form_state, $get_delta);
  54. // dsm($elements);
  55. // Arrange the form object to remove draggable table stuff
  56. $elements['widget']['#cardinality_multiple'] = FALSE;
  57. for ($i=0; $i <= $delta ; $i++) {
  58. if(isset($elements['widget'][$i]['_weight'])){
  59. $elements['widget'][$i]['_weight']['#type'] = 'hidden';
  60. }
  61. }
  62. return $elements;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
  68. $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
  69. // get default values
  70. $target_id = isset($items[$delta]->target_id) ? $items[$delta]->target_id : 0;
  71. $location = isset($items[$delta]->location) ? $items[$delta]->location : '';
  72. // dsm($target_id);
  73. // return nothing if target_id is null
  74. if(!$target_id) return;
  75. $term = Term::load($target_id);
  76. // translate the term
  77. $term = \Drupal::service('entity.repository')->getTranslationFromContext($term, $language);
  78. // dsm($element);
  79. // $element['#attributes'] = array('class' => array('container-inline'));
  80. // $element['container'] = [
  81. // '#type' => 'container',
  82. // '#field_prefix' => '<div class="container-inline">',
  83. // '#field_suffix' => '</div>',
  84. // ];
  85. $element['target_id'] = [
  86. '#type' => 'hidden',
  87. '#default_value' => $target_id,
  88. ];
  89. $element['location'] = [
  90. '#title' => $term->getName(),
  91. '#type' => 'textfield',
  92. '#default_value' => $location,
  93. '#size' => 10,
  94. '#maxlength' => 15,
  95. '#attributes' => ['class' => ['container-inline']],
  96. ];
  97. // return ['value' => $element];
  98. return $element;
  99. }
  100. public function validate($element, FormStateInterface $form_state) {
  101. // dsm($element);
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
  107. // foreach ($values as $key => $value) {
  108. // $values[$key]['value']['target_id'] = (int)$values[$key]['value']['target_id'];
  109. // }
  110. dsm($values);
  111. return $values;
  112. }
  113. }