You are here

function paragraphs_library_preprocess_paragraph in Paragraphs 8

Implements hook_preprocess_HOOK().

File

modules/paragraphs_library/paragraphs_library.module, line 139
Main module file for the Paragraphs Library module.

Code

function paragraphs_library_preprocess_paragraph(&$variables) {
  if (isset($variables['paragraph']) && $variables['paragraph']
    ->getType() == 'from_library' && $variables['paragraph']
    ->hasField('field_reusable_paragraph') && $variables['paragraph']->field_reusable_paragraph->entity) {
    $library_item = $variables['paragraph']->field_reusable_paragraph->entity;

    // Only replace the content if access is allowed to the library. Access
    // cacheability metadata is already returned by the original widget in case
    // access is not allowed.
    if ($library_item
      ->access('view') && isset($variables['elements']['field_reusable_paragraph'][0]['#view_mode'])) {
      $view_builder = \Drupal::entityTypeManager()
        ->getViewBuilder('paragraphs_library_item');
      $library_item_render_array = $view_builder
        ->view($library_item, $variables['elements']['field_reusable_paragraph'][0]['#view_mode']);

      // This will remove all fields other then field_reusable_paragraph.
      $build = $view_builder
        ->build($library_item_render_array);
      if (!empty($build['paragraphs'])) {
        $variables['content'] = $build['paragraphs'];
      }
    }
  }
}