You are here

public function ParagraphSelection::buildConfigurationForm in Paragraphs 8

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides DefaultSelection::buildConfigurationForm

File

src/Plugin/EntityReferenceSelection/ParagraphSelection.php, line 36

Class

ParagraphSelection
Default plugin implementation of the Entity Reference Selection plugin.

Namespace

Drupal\paragraphs\Plugin\EntityReferenceSelection

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $entity_type_id = $this->configuration['target_type'];
  $bundles = $this->entityTypeBundleInfo
    ->getBundleInfo($entity_type_id);
  $bundle_options = array();
  $bundle_options_simple = array();

  // Default weight for new items.
  $weight = count($bundles) + 1;
  foreach ($bundles as $bundle_name => $bundle_info) {
    $bundle_options_simple[$bundle_name] = $bundle_info['label'];
    $bundle_options[$bundle_name] = array(
      'label' => $bundle_info['label'],
      'enabled' => $this->configuration['target_bundles_drag_drop'][$bundle_name]['enabled'] ?? FALSE,
      'weight' => $this->configuration['target_bundles_drag_drop'][$bundle_name]['weight'] ?? $weight,
    );
    $weight++;
  }

  // Do negate the selection.
  $form['negate'] = [
    '#type' => 'radios',
    '#options' => [
      1 => $this
        ->t('Exclude the selected below'),
      0 => $this
        ->t('Include the selected below'),
    ],
    '#title' => $this
      ->t('Which Paragraph types should be allowed?'),
    '#default_value' => $this->configuration['negate'],
  ];

  // Kept for compatibility with other entity reference widgets.
  $form['target_bundles'] = array(
    '#type' => 'checkboxes',
    '#options' => $bundle_options_simple,
    '#default_value' => $this->configuration['target_bundles'] ?? [],
    '#access' => FALSE,
  );
  if ($bundle_options) {
    $form['target_bundles_drag_drop'] = [
      '#element_validate' => [
        [
          __CLASS__,
          'targetTypeValidate',
        ],
      ],
      '#type' => 'table',
      '#header' => [
        $this
          ->t('Type'),
        $this
          ->t('Weight'),
      ],
      '#attributes' => [
        'id' => 'bundles',
      ],
      '#prefix' => '<h5>' . $this
        ->t('Paragraph types') . '</h5>',
      '#suffix' => '<div class="description">' . $this
        ->t('Selection of Paragraph types for this field. Select none to allow all Paragraph types.') . '</div>',
    ];
    $form['target_bundles_drag_drop']['#tabledrag'][] = [
      'action' => 'order',
      'relationship' => 'sibling',
      'group' => 'bundle-weight',
    ];
  }
  uasort($bundle_options, 'Drupal\\Component\\Utility\\SortArray::sortByWeightElement');
  $weight_delta = $weight;

  // Default weight for new items.
  $weight = count($bundles) + 1;
  foreach ($bundle_options as $bundle_name => $bundle_info) {
    $form['target_bundles_drag_drop'][$bundle_name] = array(
      '#attributes' => array(
        'class' => array(
          'draggable',
        ),
      ),
    );
    $form['target_bundles_drag_drop'][$bundle_name]['enabled'] = array(
      '#type' => 'checkbox',
      '#title' => $bundle_info['label'],
      '#title_display' => 'after',
      '#default_value' => $bundle_info['enabled'],
    );
    $form['target_bundles_drag_drop'][$bundle_name]['weight'] = array(
      '#type' => 'weight',
      '#default_value' => (int) $bundle_info['weight'],
      '#delta' => $weight_delta,
      '#title' => $this
        ->t('Weight for type @type', array(
        '@type' => $bundle_info['label'],
      )),
      '#title_display' => 'invisible',
      '#attributes' => array(
        'class' => array(
          'bundle-weight',
          'bundle-weight-' . $bundle_name,
        ),
      ),
    );
    $weight++;
  }
  if (empty($bundle_options)) {
    $form['allowed_bundles_explain'] = [
      '#type' => 'markup',
      '#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(),
      ]),
    ];
  }
  return $form;
}