You are here

public function GridStackLayoutDeprecated::buildConfigurationForm in GridStack 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 LayoutBase::buildConfigurationForm

File

src/Layout/GridStackLayoutDeprecated.php, line 176

Class

GridStackLayoutDeprecated
Provides a GridStack class for Layout plugins.

Namespace

Drupal\gridstack\Layout

Code

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

  // This form may be loaded as a subform by Field Layout, Panels, etc.
  // @see https://www.drupal.org/node/2536646
  // @see https://www.drupal.org/node/2798261
  // @see https://www.drupal.org/node/2774077
  // @todo: Remove when no more issues with it.
  if ($form_state instanceof SubformStateInterface) {
    $form_state = $form_state
      ->getCompleteFormState();
  }
  $access = $this->currentUser
    ->hasPermission('administer gridstack');
  $config = $this
    ->getConfiguration();
  $definition = $this
    ->getPluginDefinition();
  $regions = $this
    ->getRegionDefinitions();
  $name = $definition['optionset'];
  $optionset = GridStack::load($name);
  $regions_all = self::prepareRegions($optionset, FALSE);
  $extras = [];

  /** @var \Drupal\field_ui\Form\EntityViewDisplayEditForm $entity_form */
  $entity_form = $form_state
    ->getFormObject();

  /* @var \Drupal\Core\Entity\Display\EntityDisplayInterface $display */
  if (method_exists($entity_form, 'getEntity') && ($display = $entity_form
    ->getEntity())) {
    $extras = [
      'bundle' => $display
        ->getTargetBundle(),
      'entity_type' => $display
        ->getTargetEntityTypeId(),
      'view_mode' => $display
        ->getMode(),
    ];
  }
  $settings = [];
  foreach ([
    'attributes',
    'wrapper_classes',
    'skin',
    'wrapper',
  ] as $key) {
    $default = $key == 'wrapper' ? 'div' : '';
    $default = isset($config[$key]) ? $config[$key] : $default;
    $settings[$key] = $form_state
      ->getValue([
      'settings',
      $key,
    ], $default);
  }
  $prefix = '<h3>';
  if ($this->manager
    ->getModuleHandler()
    ->moduleExists('gridstack_ui') && $access) {
    $prefix .= $this
      ->t('Outer wrapper settings [<small><a href=":url">Edit @id</a></small>]', [
      ':url' => $optionset
        ->toUrl('edit-form')
        ->toString(),
      '@id' => strip_tags($optionset
        ->label()),
    ]);
  }
  else {
    $prefix .= $this
      ->t('Outer wrapper settings');
  }
  $prefix .= '</h3>';
  $form['settings'] = [
    '#type' => 'container',
    '#tree' => TRUE,
    '#weight' => 20,
    '#prefix' => $prefix,
  ];
  $form['settings']['skin'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Skin'),
    '#options' => $this->manager
      ->getSkinOptions(),
    '#empty_option' => $this
      ->t('- None -'),
    '#description' => $this
      ->t('Choose a skin to load for this layout. Check out, or clone, gridstack_example.module to register skins here. Leave empty to disable.'),
    '#default_value' => $settings['skin'],
    '#prefix' => '<div class="form-wrapper form-wrapper--inline form-wrapper--left">',
  ];
  $form['settings']['extras'] = [
    '#type' => 'hidden',
    '#value' => empty($extras) ? '' : Json::encode($extras),
  ];
  $form['settings'] += $this
    ->buildFormElements($settings);
  $closing = '</div><div class="form-wrapper form-wrapper--inline form-wrapper--icon form-wrapper--right">';
  if ($uri = $optionset
    ->getIconUri()) {
    $image = [
      '#theme' => 'image',
      '#uri' => $uri,
      '#alt' => $this
        ->t('Thumbnail'),
    ];
    $closing .= $this->manager
      ->getRenderer()
      ->render($image);
  }
  $closing .= '</div>';
  $form['settings']['attributes']['#suffix'] = $closing;
  $form['regions'] = [
    '#type' => 'container',
    '#tree' => TRUE,
    '#prefix' => '<p>' . $this
      ->t('A region has direct contents. A container contains a single, or multiple regions, grouped by their container index.') . '</p>',
  ];
  $settings = [];
  foreach ($regions_all as $region => $info) {
    foreach ([
      'attributes',
      'wrapper_classes',
      'wrapper',
    ] as $key) {
      $default = $key == 'wrapper' ? 'div' : '';
      $default = isset($config['regions'][$region][$key]) ? $config['regions'][$region][$key] : $default;
      $default = $form_state
        ->getValue([
        'regions',
        $region,
        $key,
      ], $default);
      $settings['regions'][$region][$key] = $default;
    }
    $prefix = !array_key_exists($region, $regions) ? 'Container' : 'Region';
    $form['regions'][$region] = [
      '#type' => 'details',
      '#title' => $this
        ->t('@prefix: <em>@label</em>', [
        '@prefix' => $prefix,
        '@label' => $info['label'],
      ]),
      '#open' => TRUE,
      '#tree' => TRUE,
      '#attributes' => [
        'class' => [
          'form-wrapper',
          'form-wrapper--inline',
        ],
      ],
    ];
    $form['regions'][$region] += $this
      ->buildFormElements($settings['regions'][$region]);
  }
  $form['#attached']['library'][] = 'gridstack/admin_base';
  return $form;
}