You are here

public function ErlLayout::processConfigurationForm in Entity Reference with Layout 8

Add the options.

Parameters

array $form_item: The Form Item.

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

File

modules/erl_layouts/src/Plugin/Layout/ErlLayout.php, line 83

Class

ErlLayout
Layout class with various formatting options for custom ERL layouts.

Namespace

Drupal\erl_layouts\Plugin\Layout

Code

public function processConfigurationForm(array $form_item, FormStateInterface $form_state) {
  $form_object = $form_state
    ->getFormObject();
  if (!$form_object instanceof ContentEntityFormInterface) {
    return NULL;
  }
  $display = $form_object
    ->getFormDisplay($form_state);
  $component = $display
    ->getComponent($form_item['#parents'][0]);

  // @todo There must be a better way to set third_party_settings defaults!
  $settings = [
    'layout_class_mode' => 'manual',
    'layout_color_mode' => 'manual',
    'region_class_mode' => 'manual',
    'region_color_mode' => 'manual',
  ];
  if (isset($component['third_party_settings']['erl_layouts'])) {
    $settings = $component['third_party_settings']['erl_layouts'];
  }
  $config = $this
    ->getConfiguration();
  switch ($settings['layout_class_mode']) {
    case 'manual':
      $form_item['layout_classes'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Classes'),
        '#default_value' => !empty($config['layout_classes']) ? $config['layout_classes'] : '',
        '#description' => $this
          ->t('Add classes to this layout. The classes must be separated by a space.'),
        '#size' => '100',
      ];
      break;
    case 'select':
      $classes_available = $this
        ->getSelectOptions($settings['layout_class_select']);
      $default_value = !empty($config['layout_classes']) ? $config['layout_classes'] : '';
      $form_item['layout_classes'] = [
        '#type' => 'select',
        '#multiple' => TRUE,
        '#title' => $this
          ->t('Classes'),
        '#options' => $classes_available,
        '#default_value' => $default_value,
      ];
      break;
    case 'force':
      $form_item['layout']['layout_classes'] = [
        '#type' => 'value',
        '#value' => $settings['layout_class_force'],
      ];
      break;
  }
  switch ($settings['layout_color_mode']) {
    case 'manual':
      $form_item['layout_bg_color'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Background color'),
        '#default_value' => !empty($config['layout_bg_color']) ? $config['layout_bg_color'] : '',
        '#description' => $this
          ->t('Background color (hex code) for this layout.'),
        '#attributes' => [
          'placeholder' => '#ffffff',
        ],
        '#size' => '12',
      ];
      break;
    case 'select':
      $colors_available = $this
        ->getSelectOptions($settings['layout_color_select']);
      $default_value = !empty($config['layout_bg_color']) ? $config['layout_bg_color'] : '';
      $form_item['layout_bg_color'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Background color'),
        '#options' => $colors_available,
        '#default_value' => $default_value,
      ];
      break;
    case 'force':
      $form_item['layout_bg_color'] = [
        '#type' => 'value',
        '#value' => $settings['layout_color_force'],
      ];
      break;
  }
  foreach ($this
    ->getPluginDefinition()
    ->getRegions() as $region => $region_info) {
    $region_label = $region_info['label'];
    $form_item[$region] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('@region region', [
        '@region' => $region_label,
      ]),
    ];
    $visible_items = FALSE;
    switch ($settings['region_class_mode']) {
      case 'manual':
        $form_item[$region]['classes'] = [
          '#type' => 'textfield',
          '#title' => $this
            ->t('Classes'),
          '#default_value' => !empty($config[$region]['classes']) ? $config[$region]['classes'] : '',
          '#description' => $this
            ->t('Add classes to this region. The classes must be separated by a space.'),
          '#size' => '100',
        ];
        $visible_items = TRUE;
        break;
      case 'select':
        $classes_available = $this
          ->getSelectOptions($settings['region_class_select']);
        $default_value = !empty($config[$region]['classes']) ? $config[$region]['classes'] : [];
        $form_item[$region]['classes'] = [
          '#type' => 'select',
          '#multiple' => TRUE,
          '#title' => $this
            ->t('Classes'),
          '#options' => $classes_available,
          '#default_value' => $default_value,
        ];
        $visible_items = TRUE;
        break;
      case 'force':
        $form_item[$region]['classes'] = [
          '#type' => 'value',
          '#value' => $settings['region_class_force'],
        ];
        break;
    }
    switch ($settings['region_color_mode']) {
      case 'manual':
        $form_item[$region]['bg_color'] = [
          '#type' => 'textfield',
          '#title' => $this
            ->t('@region_label region background color', [
            '@region_label' => $region_label,
          ]),
          '#default_value' => !empty($config[$region]['bg_color']) ? $config[$region]['bg_color'] : '',
          '#description' => $this
            ->t('Background color (hex code) for this region.'),
          '#attributes' => [
            'placeholder' => '#ffffff',
          ],
          '#size' => '12',
        ];
        $visible_items = TRUE;
        break;
      case 'select':
        $colors_available = $this
          ->getSelectOptions($settings['region_color_select']);
        $default_value = !empty($config[$region]['bg_color']) ? $config[$region]['bg_color'] : '';
        $form_item[$region]['bg_color'] = [
          '#type' => 'select',
          '#title' => $this
            ->t('Background color'),
          '#options' => [
            '' => $this
              ->t('- None -'),
          ] + $colors_available,
          '#default_value' => $default_value,
        ];
        $visible_items = TRUE;
        break;
      case 'force':
        $form_item[$region]['bg_color'] = [
          '#type' => 'value',
          '#value' => $settings['region_color_force'],
        ];
        break;
    }
    if (!$visible_items) {
      $form_item[$region]['#access'] = FALSE;
    }
  }
  return $form_item;
}