You are here

paragraphs_viewmode.module in Paragraphs View Modes 8

Contains paragraph_viewmode.module.

File

paragraphs_viewmode.module
View source
<?php

/**
 * @file
 * Contains paragraph_viewmode.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraphs_viewmode\ParagraphsViewmodeBehaviorInterface;

/**
 * Implements hook_help().
 */
function paragraphs_viewmode_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the paragraph_viewmode module.
    case 'help.page.paragraph_viewmode':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Allows you to select the paragraph view mode on a per paragraph basis') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_entity_view_mode_alter().
 */
function paragraphs_viewmode_entity_view_mode_alter(&$view_mode, EntityInterface $entity, $context) {
  if (!$entity instanceof ParagraphInterface) {
    return;
  }
  $paragraph_type = $entity
    ->getParagraphType();
  foreach ($paragraph_type
    ->getEnabledBehaviorPlugins() as $plugin) {
    if ($plugin instanceof ParagraphsViewmodeBehaviorInterface) {
      $plugin
        ->entityViewModeAlter($view_mode, $entity, $context);
    }
  }
}