function protected function BootstrapPanel::preprocessVariables

8.x-3.x BootstrapPanel.php protected BootstrapPanel::preprocessVariables(Variables $variables)

Preprocess the variables array.

Parameters

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

Overrides PreprocessBase::preprocessVariables

Class

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

Namespace

Drupal\bootstrap\Plugin\Preprocess

Source src/Plugin/Preprocess/BootstrapPanel.php (line 97)

protected function preprocessVariables(Variables $variables) {
  // Retrieve the ID, generating one if needed.
  $id = $variables->getAttribute('id', Html::getUniqueId($variables->offsetGet('id', 'bootstrap-panel')));
  unset($variables['id']);

  // Handle collapsible state.
  if ($variables['heading'] && $variables['collapsible']) {
    // Retrieve the body ID attribute.
    if ($body_id = $variables->getAttribute('id', "$id--content", 'body_attributes')) {
      // Ensure the target is set.
      if ($variables['target'] = $variables->offsetGet('target', "#$body_id")) {
        // Set additional necessary attributes to the heading.
        $variables->setAttributes([
          'aria-controls' => preg_replace('/^#/', '', $variables['target']),
          'aria-expanded' => !$variables['collapsed'] ? 'true' : 'false',
          'aria-pressed' => !$variables['collapsed'] ? 'true' : 'false',
          'data-toggle' => 'collapse',
          'role' => 'button',
        ], 'heading_attributes');
      }
    }
  }

  // Ensure we render HTML from heading.
  $heading = $variables->offsetGet('heading');
  if ($heading && (is_string($heading) || ($heading instanceof MarkupInterface))) {
    $variables->offsetSet('heading', ['#markup' => $heading]);
  }

  // Ensure there is a valid panel state.
  if (!$variables->offsetGet('panel_type')) {
    $variables->offsetSet('panel_type', 'default');
  }

  // Convert the description variable.
  $this->preprocessDescription();

  // Ensure all attributes are proper objects.
  $this->preprocessAttributes();
}