You are here

protected function BootstrapPanel::preprocessElement in Express 8

Preprocess the variables array if an element is present.

Parameters

\Drupal\bootstrap\Utility\Element $element: The Element object.

\Drupal\bootstrap\Utility\Variables $variables: The Variables object.

Overrides PreprocessBase::preprocessElement

File

themes/contrib/bootstrap/src/Plugin/Preprocess/BootstrapPanel.php, line 27
Contains \Drupal\bootstrap\Plugin\Preprocess\BootstrapPanel.

Class

BootstrapPanel
Pre-processes variables for the "bootstrap_panel" theme hook.

Namespace

Drupal\bootstrap\Plugin\Preprocess

Code

protected function preprocessElement(Element $element, Variables $variables) {

  // Assign the ID, if not already set.
  $element
    ->map([
    'id',
  ]);

  // Add necessary classes.
  $element
    ->addClass([
    'form-item',
    'js-form-item',
    'form-wrapper',
    'js-form-wrapper',
  ]);
  $body = [];
  $properties = [
    'field_prefix',
    'body',
    'children',
  ];

  // Only add the #value property if it's a "details" or "fieldset" element
  // type. Some form elements may use "CompositeFormElementTrait" which
  // will inadvertently and eventually become preprocessed here and #value
  // may actually be the element's value instead of a renderable element.
  if ($element
    ->isType([
    'details',
    'fieldset',
  ])) {
    $properties[] = 'value';
  }

  // Add the "#field_suffix" property.
  $properties[] = 'field_suffix';

  // Merge all possible content from the element into a single render array.
  foreach ($properties as $property) {
    $body[$property] = Element::create($element
      ->getProperty($property, []))
      ->getArray();
  }
  $variables['body'] = array_filter($body);
  $map = [
    'attributes' => 'attributes',
    'body_attributes' => 'body_attributes',
    'content_attributes' => 'body_attributes',
    'description' => 'description',
    'description_attributes' => 'description_attributes',
    'description_display' => 'description_display',
    'errors' => 'errors',
    'footer' => 'footer',
    'required' => 'required',
    'panel_type' => 'panel_type',
    'title' => 'heading',
    'title_attributes' => 'heading_attributes',
  ];

  // Handle specific "details" elements.
  if ($element
    ->isType('details')) {

    // Details are always collapsible per the HTML5 spec.
    // @see https://www.drupal.org/node/1852020
    $variables['collapsible'] = TRUE;

    // Determine the collapsed state.
    $variables['collapsed'] = !$element
      ->getProperty('open', TRUE);

    // Remove the unnecessary details attribute.
    $element
      ->removeAttribute('open');
  }
  elseif ($element
    ->isType('fieldset')) {

    // Override variables to mimic the default "fieldset" element info.
    // They will be mapped below if they exist on the element.
    unset($variables['collapsible'], $variables['collapsed']);
    $map['collapsed'] = 'collapsed';
    $map['collapsible'] = 'collapsible';
  }

  // Map the element properties to the variables array.
  $variables
    ->map($map);
}