public function ParagraphsStylePlugin::view

Extends the paragraph render array with behavior.

Parameters

array &$build: A renderable array representing the paragraph. The module may add elements to $build prior to rendering. The structure of $build is a renderable array as expected by drupal_render().

\Drupal\paragraphs\Entity\Paragraph $paragraph: The paragraph.

\Drupal\Core\Entity\Display\EntityViewDisplayInterface $display: The entity view display holding the display options configured for the entity components.

string $view_mode: The view mode the entity is rendered in.

Overrides ParagraphsBehaviorInterface::view

File

paragraphs_collection/src/Plugin/paragraphs/Behavior/ParagraphsStylePlugin.php, line 262

Class

ParagraphsStylePlugin
Provides style selection plugin.

Namespace

Drupal\paragraphs_collection\Plugin\paragraphs\Behavior

Code

public function view(array &$build, Paragraph $paragraph, EntityViewDisplayInterface $display, $view_mode) {

  // Get all config for all styles and if it is not set, fallback to the
  // default style. If default style is set to none, no style will be applied.
  $paragraph_styles = $this
    ->getStyles($paragraph);
  foreach ($paragraph_styles as $enabled_style) {
    $style = $this->yamlStyleDiscovery
      ->getStyle($enabled_style);
    if ($style) {
      $build['#attributes']['class'][] = 'paragraphs-behavior-' . $this
        ->getPluginId() . '--' . $style['name'];
      if (!isset($build['#attached']['library'])) {
        $build['#attached']['library'] = [];
      }
      $build['#attached']['library'] = array_merge($style['libraries'], $build['#attached']['library']);

      // Add CSS classes from style configuration if they are defined.
      if (!empty($style['classes'])) {
        $build['#attributes']['class'] = array_merge($style['classes'], $build['#attributes']['class']);
      }

      // Add attributes defined in the configuration files to the #attributes array.
      if (!empty($style['attributes'])) {
        $build['#attributes'] = array_merge($style['attributes'], $build['#attributes']);
      }
    }
  }
}