3 namespace Drupal\paragraphs\Plugin\EntityReferenceSelection;
5 use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Component\Utility\NestedArray;
11 * Default plugin implementation of the Entity Reference Selection plugin.
13 * @EntityReferenceSelection(
14 * id = "default:paragraph",
15 * label = @Translation("Paragraphs"),
17 * entity_types = {"paragraph"},
21 class ParagraphSelection extends DefaultSelection {
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);
31 // Merge-in default values.
32 $selection_handler_settings += array(
33 'target_bundles' => array(),
34 'target_bundles_drag_drop' => array(),
37 $bundle_options = array();
38 $bundle_options_simple = array();
40 // Default weight for new items.
41 $weight = count($bundles) + 1;
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,
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(),
61 if ($bundle_options) {
62 $form['target_bundles_drag_drop'] = [
63 '#element_validate' => [[__CLASS__, 'targetTypeValidate']],
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>',
76 $form['target_bundles_drag_drop']['#tabledrag'][] = [
78 'relationship' => 'sibling',
79 'group' => 'bundle-weight',
83 uasort($bundle_options, 'Drupal\Component\Utility\SortArray::sortByWeightElement');
85 $weight_delta = $weight;
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'),
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'],
103 $form['target_bundles_drag_drop'][$bundle_name]['weight'] = array(
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),
116 if (!count($bundle_options)) {
117 $form['allowed_bundles_explain'] = [
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()]),
127 * Validate helper to have support for other entity reference widgets.
130 * @param FormStateInterface $form_state
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();
138 if ($element_values) {
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;
147 // All disabled = all enabled.
148 if ($enabled === 0) {
149 $bundle_options = NULL;
153 // New value parents.
154 $parents = array_merge(array_slice($element['#parents'], 0, -1), array('target_bundles'));
155 NestedArray::setValue($values, $parents, $bundle_options);