You are here

public function HtmlElement::preRender in Field Group 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/field_group/FieldGroupFormatter/HtmlElement.php \Drupal\field_group\Plugin\field_group\FieldGroupFormatter\HtmlElement::preRender()

Allows the field group formatter to manipulate the field group array and attach the formatters rendering element.

Parameters

array $element: The field group render array.

object $rendering_object: The object / entity beïng rendered.

Overrides FieldGroupFormatterBase::preRender

File

src/Plugin/field_group/FieldGroupFormatter/HtmlElement.php, line 28

Class

HtmlElement
Plugin implementation of the 'html_element' formatter.

Namespace

Drupal\field_group\Plugin\field_group\FieldGroupFormatter

Code

public function preRender(&$element, $rendering_object) {
  parent::preRender($element, $rendering_object);
  $element_attributes = new Attribute();
  if ($this
    ->getSetting('attributes')) {

    // This regex split the attributes string so that we can pass that
    // later to drupal_attributes().
    preg_match_all('/([^\\s=]+)="([^"]+)"/', $this
      ->getSetting('attributes'), $matches);

    // Put the attribute and the value together.
    foreach ($matches[1] as $key => $attribute) {
      $element_attributes[$attribute] = $matches[2][$key];
    }
  }

  // Add the id to the attributes array.
  if ($this
    ->getSetting('id')) {
    $element_attributes['id'] = Html::getId($this
      ->getSetting('id'));
  }

  // Add the classes to the attributes array.
  $classes = $this
    ->getClasses();
  if (!empty($classes)) {
    if (!isset($element_attributes['class'])) {
      $element_attributes['class'] = array();
    }
    else {
      $element_attributes['class'] = array(
        $element_attributes['class'],
      );
    }
    $element_attributes['class'] = array_merge($classes, $element_attributes['class']
      ->value());
  }
  $element['#effect'] = $this
    ->getSetting('effect');
  $element['#speed'] = $this
    ->getSetting('speed');
  $element['#type'] = 'field_group_html_element';
  $element['#wrapper_element'] = $this
    ->getSetting('element');
  $element['#attributes'] = $element_attributes;
  if ($this
    ->getSetting('show_label')) {
    $element['#title_element'] = $this
      ->getSetting('label_element');
    $element['#title'] = Html::escape($this
      ->t($this
      ->getLabel()));
  }
  $form_state = new FormState();
  \Drupal\field_group\Element\HtmlElement::processHtmlElement($element, $form_state);
  if ($this
    ->getSetting('required_fields')) {
    $element['#attributes']['class'][] = 'field-group-html-element';
    $element['#attached']['library'][] = 'field_group/formatter.html_element';
    $element['#attached']['library'][] = 'field_group/core';
  }
}