Version 1
[yaffs-website] / web / modules / contrib / paragraphs / src / Plugin / EntityReferenceSelection / ParagraphSelection.php
1 <?php
2
3 namespace Drupal\paragraphs\Plugin\EntityReferenceSelection;
4
5 use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
6 use Drupal\Core\Url;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Component\Utility\NestedArray;
9
10 /**
11  * Default plugin implementation of the Entity Reference Selection plugin.
12  *
13  * @EntityReferenceSelection(
14  *   id = "default:paragraph",
15  *   label = @Translation("Paragraphs"),
16  *   group = "default",
17  *   entity_types = {"paragraph"},
18  *   weight = 0
19  * )
20  */
21 class ParagraphSelection extends DefaultSelection {
22
23   /**
24    * {@inheritdoc}
25    */
26   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
27     $entity_type_id = $this->configuration['target_type'];
28     $selection_handler_settings = $this->configuration['handler_settings'] ?: array();
29     $bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type_id);
30
31     // Merge-in default values.
32     $selection_handler_settings += array(
33       'target_bundles' => array(),
34       'target_bundles_drag_drop' => array(),
35     );
36
37     $bundle_options = array();
38     $bundle_options_simple = array();
39
40     // Default weight for new items.
41     $weight = count($bundles) + 1;
42
43     foreach ($bundles as $bundle_name => $bundle_info) {
44       $bundle_options_simple[$bundle_name] = $bundle_info['label'];
45       $bundle_options[$bundle_name] = array(
46         'label' => $bundle_info['label'],
47         'enabled' => isset($selection_handler_settings['target_bundles_drag_drop'][$bundle_name]['enabled']) ? $selection_handler_settings['target_bundles_drag_drop'][$bundle_name]['enabled'] : FALSE,
48         'weight' => isset($selection_handler_settings['target_bundles_drag_drop'][$bundle_name]['weight']) ? $selection_handler_settings['target_bundles_drag_drop'][$bundle_name]['weight'] : $weight,
49       );
50       $weight++;
51     }
52
53     // Kept for compatibility with other entity reference widgets.
54     $form['target_bundles'] = array(
55       '#type' => 'checkboxes',
56       '#options' => $bundle_options_simple,
57       '#default_value' => isset($selection_handler_settings['target_bundles']) ? $selection_handler_settings['target_bundles'] : array(),
58       '#access' => FALSE,
59     );
60
61     if ($bundle_options) {
62       $form['target_bundles_drag_drop'] = [
63         '#element_validate' => [[__CLASS__, 'targetTypeValidate']],
64         '#type' => 'table',
65         '#header' => [
66           $this->t('Type'),
67           $this->t('Weight'),
68         ],
69         '#attributes' => [
70           'id' => 'bundles',
71         ],
72         '#prefix' => '<h5>' . $this->t('Paragraph types') . '</h5>',
73         '#suffix' => '<div class="description">' . $this->t('The paragraph types that are allowed to be created in this field. Select none to allow all paragraph types.') .'</div>',
74       ];
75
76       $form['target_bundles_drag_drop']['#tabledrag'][] = [
77         'action' => 'order',
78         'relationship' => 'sibling',
79         'group' => 'bundle-weight',
80       ];
81     }
82
83     uasort($bundle_options, 'Drupal\Component\Utility\SortArray::sortByWeightElement');
84
85     $weight_delta = $weight;
86
87     // Default weight for new items.
88     $weight = count($bundles) + 1;
89     foreach ($bundle_options as $bundle_name => $bundle_info) {
90       $form['target_bundles_drag_drop'][$bundle_name] = array(
91         '#attributes' => array(
92           'class' => array('draggable'),
93         ),
94       );
95
96       $form['target_bundles_drag_drop'][$bundle_name]['enabled'] = array(
97         '#type' => 'checkbox',
98         '#title' => $bundle_info['label'],
99         '#title_display' => 'after',
100         '#default_value' => $bundle_info['enabled'],
101       );
102
103       $form['target_bundles_drag_drop'][$bundle_name]['weight'] = array(
104         '#type' => 'weight',
105         '#default_value' => (int) $bundle_info['weight'],
106         '#delta' => $weight_delta,
107         '#title' => $this->t('Weight for type @type', array('@type' => $bundle_info['label'])),
108         '#title_display' => 'invisible',
109         '#attributes' => array(
110           'class' => array('bundle-weight', 'bundle-weight-' . $bundle_name),
111         ),
112       );
113       $weight++;
114     }
115
116     if (!count($bundle_options)) {
117       $form['allowed_bundles_explain'] = [
118         '#type' => 'markup',
119         '#markup' => $this->t('You did not add any paragraph types yet, click <a href=":here">here</a> to add one.', [':here' => Url::fromRoute('paragraphs.type_add')->toString()]),
120       ];
121     }
122
123     return $form;
124   }
125
126   /**
127    * Validate helper to have support for other entity reference widgets.
128    *
129    * @param $element
130    * @param FormStateInterface $form_state
131    * @param $form
132    */
133   public static function targetTypeValidate($element, FormStateInterface $form_state, $form) {
134     $values = &$form_state->getValues();
135     $element_values = NestedArray::getValue($values, $element['#parents']);
136     $bundle_options = array();
137
138     if ($element_values) {
139       $enabled = 0;
140       foreach ($element_values as $machine_name => $bundle_info) {
141         if (isset($bundle_info['enabled']) && $bundle_info['enabled']) {
142           $bundle_options[$machine_name] = $machine_name;
143           $enabled++;
144         }
145       }
146
147       // All disabled = all enabled.
148       if ($enabled === 0) {
149         $bundle_options = NULL;
150       }
151     }
152
153     // New value parents.
154     $parents = array_merge(array_slice($element['#parents'], 0, -1), array('target_bundles'));
155     NestedArray::setValue($values, $parents, $bundle_options);
156   }
157 }