Upgraded imagemagick and manually altered pdf to image module to handle changes....
[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\EntityManagerInterface;
6 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
7 use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
8 use Drupal\Core\Extension\ModuleHandlerInterface;
9 use Drupal\Core\Session\AccountInterface;
10 use Drupal\Core\Url;
11 use Drupal\Core\Form\FormStateInterface;
12 use Drupal\Component\Utility\NestedArray;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14
15 /**
16  * Default plugin implementation of the Entity Reference Selection plugin.
17  *
18  * @EntityReferenceSelection(
19  *   id = "default:paragraph",
20  *   label = @Translation("Paragraphs"),
21  *   group = "default",
22  *   entity_types = {"paragraph"},
23  *   weight = 0
24  * )
25  */
26 class ParagraphSelection extends DefaultSelection {
27
28   /**
29    * Entity type bundle info service.
30    *
31    * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
32    */
33   public $entityTypeBundleInfo;
34
35   /**
36    * ParagraphSelection constructor.
37    *
38    * @param array $configuration
39    *   A configuration array containing information about the plugin instance.
40    * @param string $plugin_id
41    *   The plugin_id for the plugin instance.
42    * @param mixed $plugin_definition
43    *   The plugin implementation definition.
44    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
45    *   The entity manager service.
46    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
47    *   The module handler service.
48    * @param \Drupal\Core\Session\AccountInterface $current_user
49    *   The current user.
50    * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
51    *   Entity type bundle info service.
52    */
53   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, AccountInterface $current_user, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
54     parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_manager, $module_handler, $current_user);
55
56     $this->entityTypeBundleInfo = $entity_type_bundle_info;
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
63     return new static(
64       $configuration,
65       $plugin_id,
66       $plugin_definition,
67       $container->get('entity.manager'),
68       $container->get('module_handler'),
69       $container->get('current_user'),
70       $container->get('entity_type.bundle.info')
71     );
72   }
73
74   /**
75    * {@inheritdoc}
76    */
77   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
78     $entity_type_id = $this->configuration['target_type'];
79     $selection_handler_settings = $this->configuration['handler_settings'] ?: array();
80     $bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type_id);
81
82     // Merge-in default values.
83     $selection_handler_settings += array(
84       'target_bundles' => array(),
85       'negate' => 0,
86       'target_bundles_drag_drop' => array(),
87     );
88
89     $bundle_options = array();
90     $bundle_options_simple = array();
91
92     // Default weight for new items.
93     $weight = count($bundles) + 1;
94
95     foreach ($bundles as $bundle_name => $bundle_info) {
96       $bundle_options_simple[$bundle_name] = $bundle_info['label'];
97       $bundle_options[$bundle_name] = array(
98         'label' => $bundle_info['label'],
99         'enabled' => isset($selection_handler_settings['target_bundles_drag_drop'][$bundle_name]['enabled']) ? $selection_handler_settings['target_bundles_drag_drop'][$bundle_name]['enabled'] : FALSE,
100         'weight' => isset($selection_handler_settings['target_bundles_drag_drop'][$bundle_name]['weight']) ? $selection_handler_settings['target_bundles_drag_drop'][$bundle_name]['weight'] : $weight,
101       );
102       $weight++;
103     }
104
105     // Do negate the selection.
106     $form['negate'] = [
107       '#type' => 'radios',
108       '#options' => [
109         1 => $this->t('Exclude the selected below'),
110         0 => $this->t('Include the selected below'),
111       ],
112       '#title' => $this->t('Which Paragraph types should be allowed?'),
113       '#default_value' => isset($selection_handler_settings['negate']) ? $selection_handler_settings['negate'] : 0,
114     ];
115
116     // Kept for compatibility with other entity reference widgets.
117     $form['target_bundles'] = array(
118       '#type' => 'checkboxes',
119       '#options' => $bundle_options_simple,
120       '#default_value' => isset($selection_handler_settings['target_bundles']) ? $selection_handler_settings['target_bundles'] : array(),
121       '#access' => FALSE,
122     );
123
124     if ($bundle_options) {
125       $form['target_bundles_drag_drop'] = [
126         '#element_validate' => [[__CLASS__, 'targetTypeValidate']],
127         '#type' => 'table',
128         '#header' => [
129           $this->t('Type'),
130           $this->t('Weight'),
131         ],
132         '#attributes' => [
133           'id' => 'bundles',
134         ],
135         '#prefix' => '<h5>' . $this->t('Paragraph types') . '</h5>',
136         '#suffix' => '<div class="description">' . $this->t('Selection of Paragraph types for this field. Select none to allow all Paragraph types.') . '</div>',
137       ];
138
139       $form['target_bundles_drag_drop']['#tabledrag'][] = [
140         'action' => 'order',
141         'relationship' => 'sibling',
142         'group' => 'bundle-weight',
143       ];
144     }
145
146     uasort($bundle_options, 'Drupal\Component\Utility\SortArray::sortByWeightElement');
147
148     $weight_delta = $weight;
149
150     // Default weight for new items.
151     $weight = count($bundles) + 1;
152     foreach ($bundle_options as $bundle_name => $bundle_info) {
153       $form['target_bundles_drag_drop'][$bundle_name] = array(
154         '#attributes' => array(
155           'class' => array('draggable'),
156         ),
157       );
158
159       $form['target_bundles_drag_drop'][$bundle_name]['enabled'] = array(
160         '#type' => 'checkbox',
161         '#title' => $bundle_info['label'],
162         '#title_display' => 'after',
163         '#default_value' => $bundle_info['enabled'],
164       );
165
166       $form['target_bundles_drag_drop'][$bundle_name]['weight'] = array(
167         '#type' => 'weight',
168         '#default_value' => (int) $bundle_info['weight'],
169         '#delta' => $weight_delta,
170         '#title' => $this->t('Weight for type @type', array('@type' => $bundle_info['label'])),
171         '#title_display' => 'invisible',
172         '#attributes' => array(
173           'class' => array('bundle-weight', 'bundle-weight-' . $bundle_name),
174         ),
175       );
176       $weight++;
177     }
178
179     if (!count($bundle_options)) {
180       $form['allowed_bundles_explain'] = [
181         '#type' => 'markup',
182         '#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()]),
183       ];
184     }
185
186     return $form;
187   }
188
189   /**
190    * Validate helper to have support for other entity reference widgets.
191    *
192    * @param $element
193    * @param FormStateInterface $form_state
194    * @param $form
195    */
196   public static function targetTypeValidate($element, FormStateInterface $form_state, $form) {
197     $values = &$form_state->getValues();
198     $element_values = NestedArray::getValue($values, $element['#parents']);
199     $bundle_options = array();
200
201     if ($element_values) {
202       $enabled = 0;
203       foreach ($element_values as $machine_name => $bundle_info) {
204         if (isset($bundle_info['enabled']) && $bundle_info['enabled']) {
205           $bundle_options[$machine_name] = $machine_name;
206           $enabled++;
207         }
208       }
209
210       // All disabled = all enabled.
211       if ($enabled === 0) {
212         $bundle_options = NULL;
213       }
214     }
215
216     // New value parents.
217     $parents = array_merge(array_slice($element['#parents'], 0, -1), array('target_bundles'));
218     NestedArray::setValue($values, $parents, $bundle_options);
219   }
220
221   /**
222    * Returns the sorted allowed types for the field.
223    *
224    * @return array
225    *   A list of arrays keyed by the paragraph type machine name
226    *   with the following properties.
227    *     - label: The label of the paragraph type.
228    *     - weight: The weight of the paragraph type.
229    */
230   public function getSortedAllowedTypes() {
231     $return_bundles = [];
232
233     $bundles = $this->entityTypeBundleInfo->getBundleInfo('paragraph');
234     if (!empty($this->configuration['handler_settings']['target_bundles'])) {
235       if (isset($this->configuration['handler_settings']['negate']) && $this->configuration['handler_settings']['negate'] == '1') {
236         $bundles = array_diff_key($bundles, $this->configuration['handler_settings']['target_bundles']);
237       }
238       else {
239         $bundles = array_intersect_key($bundles, $this->configuration['handler_settings']['target_bundles']);
240       }
241     }
242
243     // Support for the paragraphs reference type.
244     if (!empty($this->configuration['handler_settings']['target_bundles_drag_drop'])) {
245       $drag_drop_settings = $this->configuration['handler_settings']['target_bundles_drag_drop'];
246       $max_weight = count($bundles);
247
248       foreach ($drag_drop_settings as $bundle_info) {
249         if (isset($bundle_info['weight']) && $bundle_info['weight'] && $bundle_info['weight'] > $max_weight) {
250           $max_weight = $bundle_info['weight'];
251         }
252       }
253
254       // Default weight for new items.
255       $weight = $max_weight + 1;
256       foreach ($bundles as $machine_name => $bundle) {
257         $return_bundles[$machine_name] = [
258           'label' => $bundle['label'],
259           'weight' => isset($drag_drop_settings[$machine_name]['weight']) ? $drag_drop_settings[$machine_name]['weight'] : $weight,
260         ];
261         $weight++;
262       }
263     }
264     else {
265       $weight = 0;
266
267       foreach ($bundles as $machine_name => $bundle) {
268         $return_bundles[$machine_name] = [
269           'label' => $bundle['label'],
270           'weight' => $weight,
271         ];
272
273         $weight++;
274       }
275     }
276     uasort($return_bundles, 'Drupal\Component\Utility\SortArray::sortByWeightElement');
277
278     return $return_bundles;
279   }
280
281   /**
282    * {@inheritdoc}
283    */
284   public function validateReferenceableNewEntities(array $entities) {
285     $bundles = array_keys($this->getSortedAllowedTypes());
286     return array_filter($entities, function ($entity) {
287       if (isset($bundles)) {
288         return in_array($entity->bundle(), $bundles);
289       }
290       return TRUE;
291     });
292   }
293
294   /**
295    * {@inheritdoc}
296    */
297   protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
298     $target_type = $this->configuration['target_type'];
299     $handler_settings = $this->configuration['handler_settings'];
300     $entity_type = $this->entityManager->getDefinition($target_type);
301
302     $query = $this->entityManager->getStorage($target_type)->getQuery();
303
304     // If 'target_bundles' is NULL, all bundles are referenceable, no further
305     // conditions are needed.
306     if (isset($handler_settings['target_bundles']) && is_array($handler_settings['target_bundles'])) {
307       $target_bundles = array_keys($this->getSortedAllowedTypes());
308
309       // If 'target_bundles' is an empty array, no bundle is referenceable,
310       // force the query to never return anything and bail out early.
311       if ($target_bundles === []) {
312         $query->condition($entity_type->getKey('id'), NULL, '=');
313         return $query;
314       }
315       else {
316         $query->condition($entity_type->getKey('bundle'), $target_bundles, 'IN');
317       }
318     }
319
320     if (isset($match) && $label_key = $entity_type->getKey('label')) {
321       $query->condition($label_key, $match, $match_operator);
322     }
323
324     // Add entity-access tag.
325     $query->addTag($target_type . '_access');
326
327     // Add the Selection handler for system_query_entity_reference_alter().
328     $query->addTag('entity_reference');
329     $query->addMetaData('entity_reference_selection_handler', $this);
330
331     // Add the sort option.
332     if (!empty($handler_settings['sort'])) {
333       $sort_settings = $handler_settings['sort'];
334       if ($sort_settings['field'] != '_none') {
335         $query->sort($sort_settings['field'], $sort_settings['direction']);
336       }
337     }
338
339     return $query;
340   }
341
342 }