You are here

public function ManageComponentAttributesForm::buildForm in Layout Builder Component Attributes 1.1.x

Same name and namespace in other branches
  1. 1.2.x src/Form/ManageComponentAttributesForm.php \Drupal\layout_builder_component_attributes\Form\ManageComponentAttributesForm::buildForm()
  2. 1.0.x src/Form/ManageComponentAttributesForm.php \Drupal\layout_builder_component_attributes\Form\ManageComponentAttributesForm::buildForm()

Builds the attributes form.

Parameters

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

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

\Drupal\layout_builder\SectionStorageInterface $section_storage: The section storage being configured.

int $delta: The original delta of the section.

string $uuid: The UUID of the block being updated.

Return value

array The form array.

Overrides FormInterface::buildForm

File

src/Form/ManageComponentAttributesForm.php, line 108

Class

ManageComponentAttributesForm
Provides a form for managing block attributes.

Namespace

Drupal\layout_builder_component_attributes\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, $uuid = NULL) {
  $parameters = array_slice(func_get_args(), 2);
  foreach ($parameters as $parameter) {
    if (is_null($parameter)) {
      throw new \InvalidArgumentException('ManageComponentAttributesForm requires all parameters.');
    }
  }
  $config = $this->configFactory
    ->get('layout_builder_component_attributes.settings')
    ->get();

  // Determine which categories, if any, are empty (i.e. no
  // allowed attributes).
  $categories = [
    'allowed_block_attributes',
    'allowed_block_title_attributes',
    'allowed_block_content_attributes',
  ];
  $empty_categories = [];
  foreach ($categories as $category) {
    $empty_categories[$category] = TRUE;
    foreach ($config[$category] as $value) {
      if ($value) {
        $empty_categories[$category] = FALSE;
        break;
      }
    }
  }
  $this->sectionStorage = $section_storage;
  $this->delta = $delta;
  $this->uuid = $uuid;
  $section = $section_storage
    ->getSection($delta);
  $component = $section
    ->getComponent($uuid);
  $component_attributes = $component
    ->get('component_attributes');
  $form['#attributes']['data-layout-builder-target-highlight-id'] = $this
    ->blockUpdateHighlightId($uuid);
  $form_partial_id = [
    '#type' => 'textfield',
    '#title' => 'ID',
    '#description' => $this
      ->t('An HTML identifier unique to the page.'),
  ];
  $form_partial_class = [
    '#type' => 'textfield',
    '#title' => 'Class(es)',
    '#description' => $this
      ->t('Classes to be applied. Multiple classes should be separated by a space.'),
  ];
  $form_partial_style = [
    '#type' => 'textfield',
    '#title' => 'Style',
    '#description' => $this
      ->t('Inline CSS styles. <em>In general, inline CSS styles should be avoided.</em>'),
  ];
  $form_partial_data = [
    '#type' => 'textarea',
    '#title' => 'Data-* attributes',
    '#description' => $this
      ->t('Custom attributes, which are available to both CSS and JS.<br><br>Each attribute should be entered on its own line with a pipe (|) separating its name and its optional value:<br>data-test|example-value<br>data-attribute-with-no-value'),
  ];
  if (!$empty_categories['allowed_block_attributes']) {
    $form['block_attributes'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Block attributes'),
    ];
    $form['block_attributes']['intro'] = [
      '#markup' => $this
        ->t('<p>Manage attributes on the block wrapper (outer) element</p>'),
    ];
    if ($config['allowed_block_attributes']['id']) {
      $form['block_attributes']['id'] = $form_partial_id + [
        '#default_value' => $component_attributes['block_attributes']['id'] ?? '',
      ];
    }
    if ($config['allowed_block_attributes']['class']) {
      $form['block_attributes']['class'] = $form_partial_class + [
        '#default_value' => $component_attributes['block_attributes']['class'] ?? '',
      ];
    }
    if ($config['allowed_block_attributes']['style']) {
      $form['block_attributes']['style'] = $form_partial_style + [
        '#default_value' => $component_attributes['block_attributes']['style'] ?? '',
      ];
    }
    if ($config['allowed_block_attributes']['data']) {
      $form['block_attributes']['data'] = $form_partial_data + [
        '#default_value' => $component_attributes['block_attributes']['data'] ?? '',
      ];
    }
  }
  if (!$empty_categories['allowed_block_title_attributes']) {
    $form['block_title_attributes'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Block title attributes'),
    ];
    $form['block_title_attributes']['intro'] = [
      '#markup' => $this
        ->t('<p>Manage attributes on the block title element</p>'),
    ];
    if ($config['allowed_block_title_attributes']['id']) {
      $form['block_title_attributes']['id'] = $form_partial_id + [
        '#default_value' => $component_attributes['block_title_attributes']['id'] ?? '',
      ];
    }
    if ($config['allowed_block_title_attributes']['class']) {
      $form['block_title_attributes']['class'] = $form_partial_class + [
        '#default_value' => $component_attributes['block_title_attributes']['class'] ?? '',
      ];
    }
    if ($config['allowed_block_title_attributes']['style']) {
      $form['block_title_attributes']['style'] = $form_partial_style + [
        '#default_value' => $component_attributes['block_title_attributes']['style'] ?? '',
      ];
    }
    if ($config['allowed_block_title_attributes']['data']) {
      $form['block_title_attributes']['data'] = $form_partial_data + [
        '#default_value' => $component_attributes['block_title_attributes']['data'] ?? '',
      ];
    }
  }
  if (!$empty_categories['allowed_block_content_attributes']) {
    $form['block_content_attributes'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Block content attributes'),
    ];
    $form['block_content_attributes']['intro'] = [
      '#markup' => $this
        ->t('<p>Manage attributes on the block content (inner) element</p>'),
    ];
    if ($config['allowed_block_content_attributes']['id']) {
      $form['block_content_attributes']['id'] = $form_partial_id + [
        '#default_value' => $component_attributes['block_content_attributes']['id'] ?? '',
      ];
    }
    if ($config['allowed_block_content_attributes']['class']) {
      $form['block_content_attributes']['class'] = $form_partial_class + [
        '#default_value' => $component_attributes['block_content_attributes']['class'] ?? '',
      ];
    }
    if ($config['allowed_block_content_attributes']['style']) {
      $form['block_content_attributes']['style'] = $form_partial_style + [
        '#default_value' => $component_attributes['block_content_attributes']['style'] ?? '',
      ];
    }
    if ($config['allowed_block_content_attributes']['data']) {
      $form['block_content_attributes']['data'] = $form_partial_data + [
        '#default_value' => $component_attributes['block_content_attributes']['data'] ?? '',
      ];
    }
  }
  $form['#tree'] = TRUE;
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Update'),
    '#button_type' => 'primary',
  ];
  if ($this
    ->isAjax()) {
    $form['actions']['submit']['#ajax']['callback'] = '::ajaxSubmit';
  }
  return $form;
}