You are here

public function ParagraphsGridLayoutPlugin::buildConfigurationForm in Paragraphs Collection 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 ParagraphsBehaviorBase::buildConfigurationForm

File

src/Plugin/paragraphs/Behavior/ParagraphsGridLayoutPlugin.php, line 83

Class

ParagraphsGridLayoutPlugin
Provides a way to define grid based layouts.

Namespace

Drupal\paragraphs_collection\Plugin\paragraphs\Behavior

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $paragraphs_type = $form_state
    ->getFormObject()
    ->getEntity();
  if ($paragraphs_type
    ->isNew()) {
    return [];
  }

  // The grid gets it's content from referenced entities (ERR or ER).
  $reference_field_options = [];
  $field_definitions = $this->entityFieldManager
    ->getFieldDefinitions('paragraph', $paragraphs_type
    ->id());
  foreach ($field_definitions as $name => $definition) {
    if ($definition instanceof FieldConfigInterface) {
      if ($definition
        ->getFieldStorageDefinition()
        ->getCardinality() != 1 && in_array($definition
        ->getType(), [
        'entity_reference',
        'entity_reference_revisions',
      ])) {
        $reference_field_options[$name] = $definition
          ->getLabel();
      }
    }
  }
  if ($reference_field_options) {
    $form['paragraph_reference_field'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Grid field'),
      '#description' => $this
        ->t('Field to be used as the grid container.'),
      '#options' => $reference_field_options,
      '#default_value' => $this->configuration['paragraph_reference_field'],
    ];
  }
  else {
    $url = Url::fromRoute('entity.paragraph.field_ui_fields', [
      $paragraphs_type
        ->getEntityTypeId() => $paragraphs_type
        ->id(),
    ]);
    $form['message'] = [
      '#type' => 'container',
      '#markup' => $this
        ->t('No paragraph reference field type available. Please add at least one in the <a href=":link">Manage fields</a> page.', [
        ':link' => $url
          ->toString(),
      ]),
      '#attributes' => [
        'class' => [
          'messages messages--error',
        ],
      ],
    ];
  }

  // Select pre-defined grid layouts.
  if ($layout_options = $this->gridLayoutDiscovery
    ->getLayoutOptions()) {
    $form['available_grid_layouts'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Grid layouts'),
      '#description' => $this
        ->t('Layouts that will be available when creating paragraphs. Select none to allow displaying all layouts.'),
      '#options' => $layout_options,
      '#default_value' => $this->configuration['available_grid_layouts'],
      '#empty_value' => [],
    ];
  }
  return $form;
}