You are here

public function WidgetsConfig::buildForm in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 src/Form/WidgetsConfig.php \Drupal\entity_browser\Form\WidgetsConfig::buildForm()

Form constructor.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityForm::buildForm

File

src/Form/WidgetsConfig.php, line 56

Class

WidgetsConfig
Form for configuring widgets for entity browser.

Namespace

Drupal\entity_browser\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
  $entity_browser = $this
    ->getEntity();
  $options = [
    '_none_' => '- ' . $this
      ->t('Select a widget to add it') . ' -',
  ];
  $description = [
    '#theme' => 'item_list',
    '#list_type' => 'ul',
    '#title' => $this
      ->t('The available plugins are:'),
    '#items' => [],
    '#attributes' => [
      'class' => 'widget-description-list',
    ],
  ];
  foreach ($this->widgetManager
    ->getDefinitions() as $plugin_id => $plugin_definition) {
    $options[$plugin_id] = $plugin_definition['label'];
    $description['#items'][] = [
      '#markup' => '<strong>' . $plugin_definition['label'] . ':</strong> ' . $plugin_definition['description'],
    ];
  }
  $default_widgets = [];
  foreach ($entity_browser
    ->getWidgets() as $widget) {

    /** @var \Drupal\entity_browser\WidgetInterface $widget */
    $default_widgets[] = $widget
      ->id();
  }
  $form['widget'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Add widget plugin'),
    '#options' => $options,
    '#description' => $description,
    '#ajax' => [
      'callback' => [
        get_class($this),
        'tableUpdatedAjaxCallback',
      ],
      'wrapper' => 'widgets',
      'event' => 'change',
    ],
    '#executes_submit_callback' => TRUE,
    '#submit' => [
      [
        get_class($this),
        'submitAddWidget',
      ],
    ],
    '#limit_validation_errors' => [
      [
        'widget',
      ],
    ],
  ];
  $form_state
    ->unsetValue('widget');
  $form['widgets'] = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'widgets',
    ],
  ];
  $form['widgets']['table'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Form'),
      $this
        ->t('Operations'),
      $this
        ->t('Actions'),
      $this
        ->t('Weight'),
    ],
    '#empty' => $this
      ->t('There are no widgets.'),
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'variant-weight',
      ],
    ],
  ];

  /** @var \Drupal\entity_browser\WidgetInterface $widget */
  foreach ($entity_browser
    ->getWidgets() as $uuid => $widget) {
    $row = [
      '#attributes' => [
        'class' => [
          'draggable',
        ],
      ],
    ];
    $row['label'] = [
      '#type' => 'textfield',
      '#default_value' => $widget
        ->label(),
      '#title' => $this
        ->t('Label (%label)', [
        '%label' => $widget
          ->getPluginDefinition()['label'],
      ]),
    ];
    $row['form'] = [];
    $row['form'] = $widget
      ->buildConfigurationForm($row['form'], $form_state);
    $row['remove'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Delete'),
      '#name' => 'remove' . $uuid,
      '#ajax' => [
        'callback' => [
          get_class($this),
          'tableUpdatedAjaxCallback',
        ],
        'wrapper' => 'widgets',
        'event' => 'click',
      ],
      '#executes_submit_callback' => TRUE,
      '#submit' => [
        [
          get_class($this),
          'submitDeleteWidget',
        ],
      ],
      '#arguments' => $uuid,
      '#limit_validation_errors' => [],
    ];
    $row['weight'] = [
      '#type' => 'weight',
      '#default_value' => $widget
        ->getWeight(),
      '#title' => $this
        ->t('Weight for @widget widget', [
        '@widget' => $widget
          ->label(),
      ]),
      '#title_display' => 'invisible',
      '#attributes' => [
        'class' => [
          'variant-weight',
        ],
      ],
    ];
    $form['widgets']['table'][$uuid] = $row;
  }
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => t('Save'),
  ];
  return $form;
}