You are here

public function BaseLayoutBase::buildConfigurationForm in Layout Builder Base 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 MultiWidthLayoutBase::buildConfigurationForm

File

src/Plugin/Layout/BaseLayoutBase.php, line 100

Class

BaseLayoutBase
Configurable layout plugin class.

Namespace

Drupal\layout_builder_base\Plugin\Layout

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildConfigurationForm($form, $form_state);

  // Since we extend MultiWidthLayoutBase, remove column_widths when empty.
  if (empty($this
    ->getWidthOptions())) {
    unset($form['column_widths']);
  }
  if ($this
    ->isMultipleColumnsLayout()) {
    $form['grid'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Grid properties'),
    ];
    if (!empty($this
      ->getColumnGapOptions())) {
      $form['grid']['column_gap'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Column gap'),
        '#default_value' => $this->configuration['column_gap'],
        '#options' => $this
          ->getColumnGapOptions(),
        '#description' => $this
          ->t('Choose the column gap for this layout.'),
      ];
    }
    if (!empty($this
      ->getRowGapOptions())) {
      $form['grid']['row_gap'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Row gap'),
        '#default_value' => $this->configuration['row_gap'],
        '#options' => $this
          ->getRowGapOptions(),
        '#description' => $this
          ->t('Choose the row gap for this layout.'),
      ];
    }
    if (!empty($this
      ->getColumnBreakpointOptions())) {
      $form['grid']['column_breakpoint'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Column breakpoint'),
        '#default_value' => $this->configuration['column_breakpoint'],
        '#options' => $this
          ->getColumnBreakpointOptions(),
        '#description' => $this
          ->t('Choose the column breakpoint where it comes back to one column for this layout.'),
      ];
    }
    if (!empty($this
      ->getColumnWidthOptions())) {
      $form['grid']['column_width'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Column width'),
        '#default_value' => $this->configuration['column_width'],
        '#options' => $this
          ->getColumnWidthOptions(),
        '#description' => $this
          ->t('Choose the column width for this layout.'),
      ];
    }
    if (!empty($this
      ->getAlignItemsOptions())) {
      $form['grid']['align_items'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Align items'),
        '#default_value' => $this->configuration['align_items'],
        '#options' => $this
          ->getAlignItemsOptions(),
        '#description' => $this
          ->t('Choose the align items logic for this layout.'),
      ];
    }
    if (!empty($this
      ->getCustomizableColumnsOptions())) {
      $form['grid']['customizable_columns'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Customizable columns'),
        '#default_value' => $this->configuration['customizable_columns'],
        '#options' => $this
          ->getCustomizableColumnsOptions(),
        '#description' => $this
          ->t('Choose the type of column layout.'),
      ];
    }
  }
  $background_implemented = $this
    ->enableImageBackground() || !empty($this
    ->getBackgroundOptions()) || !empty($this
    ->getBackgroundAttachmentOptions()) || !empty($this
    ->getBackgroundPositionOptions()) || !empty($this
    ->getBackgroundSizeOptions()) || !empty($this
    ->getBackgroundOverlayOptions());
  if ($background_implemented) {
    $form['background_detail'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Background'),
    ];
  }
  if ($this
    ->enableImageBackground()) {
    $form['background_detail']['background_image'] = [
      '#type' => 'managed_file',
      '#title' => $this
        ->t('Background image'),
      '#default_value' => !empty($this->configuration['background_image']) ? [
        $this->configuration['background_image'],
      ] : '',
      '#description' => $this
        ->t('Choose a background image for this layout. This feature is using inline CSS.'),
      '#upload_validators' => [
        'file_validate_is_image' => [],
        'file_validate_extensions' => [
          'gif png jpg jpeg',
        ],
      ],
      '#upload_location' => 'public://layout_background/',
    ];
    $form['background_detail']['background_image_style'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Background Image Style'),
      '#default_value' => $this->configuration['background_image_style'],
      '#options' => $this
        ->getBackgroundImageStyleOptions(),
    ];
  }
  if (!empty($this
    ->getBackgroundOptions())) {
    $form['background_detail']['background'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Background'),
      '#default_value' => $this->configuration['background'],
      '#options' => $this
        ->getBackgroundOptions(),
      '#description' => $this
        ->t('Choose the background for this layout.'),
    ];
  }
  if (!empty($this
    ->getBackgroundAttachmentOptions())) {
    $form['background_detail']['background_attachment'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Background Attachment'),
      '#default_value' => $this->configuration['background_attachment'],
      '#options' => $this
        ->getBackgroundAttachmentOptions(),
      '#description' => $this
        ->t('Choose the background attachment for this layout.'),
    ];
  }
  if (!empty($this
    ->getBackgroundPositionOptions())) {
    $form['background_detail']['background_position'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Background Position'),
      '#default_value' => $this->configuration['background_position'],
      '#options' => $this
        ->getBackgroundPositionOptions(),
      '#description' => $this
        ->t('Choose the background position for this layout.'),
    ];
  }
  if (!empty($this
    ->getBackgroundSizeOptions())) {
    $form['background_detail']['background_size'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Background Size'),
      '#default_value' => $this->configuration['background_size'],
      '#options' => $this
        ->getBackgroundSizeOptions(),
      '#description' => $this
        ->t('Choose the background size for this layout.'),
    ];
  }
  if (!empty($this
    ->getBackgroundOverlayOptions())) {
    $form['background_detail']['background_overlay'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Background Overlay'),
      '#default_value' => $this->configuration['background_overlay'],
      '#options' => $this
        ->getBackgroundOverlayOptions(),
      '#description' => $this
        ->t('Choose the background overlay for this layout.'),
    ];
  }
  $margin_implemented = !empty($this
    ->getTopMarginOptions()) || !empty($this
    ->getBottomMarginOptions()) || !empty($this
    ->getLeftMarginOptions()) || !empty($this
    ->getRightMarginOptions()) || !empty($this
    ->getEqualTopBottomMarginsOptions()) || !empty($this
    ->getEqualLeftRightMarginsOptions());
  if ($margin_implemented) {
    $form['margin'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Margins'),
    ];
  }
  if (!empty($this
    ->getTopMarginOptions())) {
    $form['margin']['top_margin'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Top Margin'),
      '#default_value' => $this->configuration['top_margin'],
      '#options' => $this
        ->getTopMarginOptions(),
      '#description' => $this
        ->t('Choose the top margin for this layout.'),
    ];
  }
  if (!empty($this
    ->getBottomMarginOptions())) {
    $form['margin']['bottom_margin'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Bottom Margin'),
      '#default_value' => $this->configuration['bottom_margin'],
      '#options' => $this
        ->getBottomMarginOptions(),
      '#description' => $this
        ->t('Choose the bottom margin for this layout.'),
    ];
  }
  if (!empty($this
    ->getLeftMarginOptions())) {
    $form['margin']['left_margin'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Left Margin'),
      '#default_value' => $this->configuration['left_margin'],
      '#options' => $this
        ->getLeftMarginOptions(),
      '#description' => $this
        ->t('Choose the left margin for this layout.'),
    ];
  }
  if (!empty($this
    ->getRightMarginOptions())) {
    $form['margin']['right_margin'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Right Margin'),
      '#default_value' => $this->configuration['right_margin'],
      '#options' => $this
        ->getRightMarginOptions(),
      '#description' => $this
        ->t('Choose the right margin for this layout.'),
    ];
  }
  if (!empty($this
    ->getEqualTopBottomMarginsOptions())) {
    $form['margin']['equal_top_bottom_margins'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Top Bottom Margins'),
      '#default_value' => $this->configuration['equal_top_bottom_margins'],
      '#options' => $this
        ->getEqualTopBottomMarginsOptions(),
      '#description' => $this
        ->t('Choose the top/bottom margins for this layout.'),
    ];
  }
  if (!empty($this
    ->getEqualLeftRightMarginsOptions())) {
    $form['margin']['equal_left_right_margins'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Left Right Margins'),
      '#default_value' => $this->configuration['equal_left_right_margins'],
      '#options' => $this
        ->getEqualLeftRightMarginsOptions(),
      '#description' => $this
        ->t('Choose the left/right margins for this layout.'),
    ];
  }
  $padding_implemented = !empty($this
    ->getTopPaddingOptions()) || !empty($this
    ->getBottomPaddingOptions()) || !empty($this
    ->getLeftPaddingOptions()) || !empty($this
    ->getRightPaddingOptions()) || !empty($this
    ->getEqualTopBottomPaddingsOptions()) || !empty($this
    ->getEqualLeftRightPaddingsOptions());
  if ($padding_implemented) {
    $form['padding'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Paddings'),
    ];
  }
  if (!empty($this
    ->getTopPaddingOptions())) {
    $form['padding']['top_padding'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Top Padding'),
      '#default_value' => $this->configuration['top_padding'],
      '#options' => $this
        ->getTopPaddingOptions(),
      '#description' => $this
        ->t('Choose the top padding for this layout.'),
    ];
  }
  if (!empty($this
    ->getBottomPaddingOptions())) {
    $form['padding']['bottom_padding'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Bottom Padding'),
      '#default_value' => $this->configuration['bottom_padding'],
      '#options' => $this
        ->getBottomPaddingOptions(),
      '#description' => $this
        ->t('Choose the bottom padding for this layout.'),
    ];
  }
  if (!empty($this
    ->getLeftPaddingOptions())) {
    $form['padding']['left_padding'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Left Padding'),
      '#default_value' => $this->configuration['left_padding'],
      '#options' => $this
        ->getLeftPaddingOptions(),
      '#description' => $this
        ->t('Choose the left padding for this layout.'),
    ];
  }
  if (!empty($this
    ->getRightPaddingOptions())) {
    $form['padding']['right_padding'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Right Padding'),
      '#default_value' => $this->configuration['right_padding'],
      '#options' => $this
        ->getRightPaddingOptions(),
      '#description' => $this
        ->t('Choose the right padding for this layout.'),
    ];
  }
  if (!empty($this
    ->getEqualTopBottomPaddingsOptions())) {
    $form['padding']['equal_top_bottom_paddings'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Top Bottom Paddings'),
      '#default_value' => $this->configuration['equal_top_bottom_paddings'],
      '#options' => $this
        ->getEqualTopBottomPaddingsOptions(),
      '#description' => $this
        ->t('Choose the top/bottom paddings for this layout.'),
    ];
  }
  if (!empty($this
    ->getEqualLeftRightPaddingsOptions())) {
    $form['padding']['equal_left_right_paddings'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Left Right Paddings'),
      '#default_value' => $this->configuration['equal_left_right_paddings'],
      '#options' => $this
        ->getEqualLeftRightPaddingsOptions(),
      '#description' => $this
        ->t('Choose the left/right paddings for this layout.'),
    ];
  }
  $container_implemented = !empty($this
    ->getHeightOptions()) || !empty($this
    ->getContainerOptions()) || !empty($this
    ->getContentContainerOptions());
  if (!empty($container_implemented)) {
    $form['container_detail'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Container'),
    ];
  }
  if (!empty($this
    ->getHeightOptions())) {
    $form['container_detail']['height'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Height'),
      '#default_value' => $this->configuration['height'],
      '#options' => $this
        ->getHeightOptions(),
      '#description' => $this
        ->t('Choose the height for this layout. It will change the minimum height of your layout.'),
    ];
  }
  if (!empty($this
    ->getContainerOptions())) {
    $form['container_detail']['container'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Container'),
      '#default_value' => $this->configuration['container'],
      '#options' => $this
        ->getContainerOptions(),
      '#description' => $this
        ->t('Choose the size of the container for this layout. It will change the maximum width of your layout.'),
    ];
  }
  if (!empty($this
    ->getContentContainerOptions())) {
    $form['container_detail']['content_container'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Content container'),
      '#default_value' => $this->configuration['content_container'],
      '#options' => $this
        ->getContentContainerOptions(),
      '#description' => $this
        ->t('Choose the size of the container for the content of this layout. It is pretty usefull if you want a full width background but contains your content in the middle.'),
    ];
  }
  $style_implemented = !empty($this
    ->getColorsOptions()) || !empty($this
    ->getAlignmentOptions()) || !empty($this
    ->getModifiersOptions());
  if ($style_implemented) {
    $form['styles'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Styles'),
    ];
  }
  if (!empty($this
    ->getColorsOptions())) {
    $form['styles']['color'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Default color'),
      '#default_value' => $this->configuration['color'],
      '#options' => $this
        ->getColorsOptions(),
      '#description' => $this
        ->t('Choose the default color for the text of this layout.'),
    ];
  }
  if (!empty($this
    ->getAlignmentOptions())) {
    $form['styles']['alignment'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Default alignment'),
      '#default_value' => $this->configuration['alignment'],
      '#options' => $this
        ->getAlignmentOptions(),
      '#description' => $this
        ->t('Choose the default alignment for the text of this layout.'),
    ];
  }
  if (!empty($this
    ->getModifiersOptions())) {
    $form['styles']['modifiers'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Modifiers'),
      '#default_value' => $this->configuration['modifiers'],
      '#options' => $this
        ->getModifiersOptions(),
      '#description' => $this
        ->t('Choose the modifiers for this layout.'),
    ];
  }
  return $form;
}