You are here

public function PatternFormatter::preRender in UI Patterns 8

File

modules/ui_patterns_field_group/src/Plugin/field_group/FieldGroupFormatter/PatternFormatter.php, line 100

Class

PatternFormatter
Plugin implementation of the 'paragraph' formatter.

Namespace

Drupal\ui_patterns_field_group\Plugin\field_group\FieldGroupFormatter

Code

public function preRender(&$element, $rendering_object) {
  $fields = [];
  $mapping = $this
    ->getSetting('pattern_mapping');
  foreach ($mapping as $field) {
    $fields[$field['destination']][] = $element[$field['source']];
  }
  $element['#type'] = 'pattern';
  $element['#id'] = $this
    ->getSetting('pattern');
  $element['#fields'] = $fields;
  $element['#multiple_sources'] = TRUE;
  $element['#variant'] = $this
    ->getSetting('pattern_variant');

  // Allow default context values to not override those exposed elsewhere.
  $element['#context']['type'] = 'field_group';
  $element['#context']['group_name'] = $this->configuration['group']->group_name;
  $element['#context']['entity_type'] = $this->configuration['group']->entity_type;
  $element['#context']['bundle'] = $this->configuration['group']->bundle;
  $element['#context']['view_mode'] = $this->configuration['group']->mode;

  // Pass current entity to pattern context, if any.
  $element['#context']['entity'] = $this->entityFinder
    ->findEntityFromFields($element['#fields']);
}