You are here

paragraphs_grid.module in Paragraphs grid 8

File

paragraphs_grid.module
View source
<?php

/**
 * @file
 * Contains paragraphs_grid.module.
 */
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function paragraphs_grid_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.paragraphs_grid':
      $text = file_get_contents(__DIR__ . '/README.md');
      if (!\Drupal::moduleHandler()
        ->moduleExists('markdown')) {
        return '<pre>' . Html::escape($text) . '</pre>';
      }
      else {

        // Use the Markdown filter to render the README.
        $filter_manager = \Drupal::service('plugin.manager.filter');
        $settings = \Drupal::configFactory()
          ->get('markdown.settings')
          ->getRawData();
        $config = [
          'settings' => $settings,
        ];
        $filter = $filter_manager
          ->createInstance('markdown', $config);
        return $filter
          ->process($text, 'en');
      }
  }
  return NULL;
}

/**
 * Implements hook_theme().
 */
function paragraphs_grid_theme() {
  return [
    'pg_button' => [
      'variables' => [
        'label' => NULL,
        'icon' => NULL,
        'attributes' => NULL,
      ],
    ],
    'pg_bpoint_col_header' => [
      'variables' => [
        'name' => NULL,
        'size' => NULL,
        'attributes' => NULL,
        'icon_attributes' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_theme_suggestions_alter().
 *
 * Add a theme hook selected by the formatter.
 */
function paragraphs_grid_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
  if ($hook == 'field' && $variables['element']['#field_type'] == 'entity_reference_revisions') {
    $suggestions[] = $hook . '__' . $variables['element']['#formatter'];
  }
}

/**
 * Implements hook_theme_registry_alter().
 */
function paragraphs_grid_theme_registry_alter(&$theme_registry) {
  $theme_registry['field__entity_reference_revisions'] = $theme_registry['field'];
  $theme_registry['field__entity_reference_revisions']['template'] = 'field--paragraphs-grid-formatter';
  $theme_registry['field__entity_reference_revisions']['path'] = drupal_get_path('module', 'paragraphs_grid') . '/templates';
}

/**
 * Implements hook_preprocess_field().
 */
function paragraphs_grid_preprocess_field(&$variables) {
  if ($variables['field_type'] == 'entity_reference_revisions' && $variables['element']['#formatter'] == 'paragraphs_grid_formatter') {

    // Get module configuration.
    $module_config = \Drupal::config('paragraphs_grid.settings');

    // Get configured grid-type-definition.
    $grid_type = $module_config
      ->get('gridtype');
    $grid_config = $grid_type ? \Drupal::config($grid_type) : NULL;

    // Set the grid wrapper classes.
    $variables['container_attributes'] = isset($variables['element']['#container_attributes']) ? $variables['element']['#container_attributes'] : new Attribute([]);
    $variables['row_attributes'] = new Attribute([]);
    if ($grid_config) {

      // Set grid row classes.
      $wrap_row = $grid_config
        ->get('wrapper.row');
      $variables['row_attributes']
        ->addClass($wrap_row['options']);

      // Set grid cell classes.
      foreach ($variables['items'] as &$item) {

        // Get cell fallback class.
        $fallback = $grid_config
          ->get('cell-fallback') ?: '';

        // Set cell classes.
        $field_grid = _paragraphs_grid_get_field_by_type($item['content']['#paragraph'], 'grid_field_type');
        $field_grid_val = $field_grid ? $field_grid->value : '';
        $classes = $field_grid_val ? explode(' ', $field_grid_val) : [];
        $classes = count($classes) ? $classes : [
          $fallback,
        ];

        /* @var Drupal\Core\Template\Attribute $item['attributes'] */
        $item['attributes']
          ->addClass($classes);
      }
    }
  }
}

/**
 * Implements hook_page_attachments().
 *
 * Append grid library.
 */
function paragraphs_grid_page_attachments(array &$page) {

  // Get module configuration.
  $module_config = \Drupal::config('paragraphs_grid.settings');
  if ($module_config
    ->get('use_lib_admin_pages') || !\Drupal::service('router.admin_context')
    ->isAdminRoute()) {

    // Get configured grid-type-definition.
    $grid_type = $module_config
      ->get('gridtype');
    $grid_config = $grid_type ? \Drupal::config($grid_type) : NULL;

    // Attach library from pg module.
    if ($module_config
      ->get('uselibrary') && ($lib = $grid_config
      ->get('library'))) {
      $page['#attached']['library'][] = $lib;
    }
  }
}

/**
 * Implements hook_entity_view_mode_alter().
 *
 * If entity is a paragraph, ...
 * find grid_field_type field, ...
 * get the field value for view mode ...
 * and finally => change the view mode of the paragraphs entity.
 */
function paragraphs_grid_entity_view_mode_alter(&$view_mode, EntityInterface $entity, $context) {
  if ($entity
    ->getEntityTypeId() == 'paragraph' && $view_mode != 'preview') {

    /** @var \Drupal\paragraphs\Entity\Paragraph $entity */

    // Find grid_field_type field, get set view mode and change the view mode.
    $fields = \Drupal::service('entity_field.manager')
      ->getFieldDefinitions($entity
      ->getEntityTypeId(), $entity
      ->bundle());
    foreach ($fields as $field_name => $field) {
      if ($field
        ->getType() == 'grid_field_type' && ($field_value = $entity
        ->get($field_name)
        ->getValue())) {
        $field_value = reset($field_value);
        $view_mode = $field_value['view_mode'] ?: $view_mode;
        break;
      }
    }
  }
}

/**
 * Get active page field.
 *
 * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
 *   The Entity which should be checked.
 *
 * @return \Drupal\Core\Field\FieldItemListInterface|null
 *   Return the field or null.
 */
function _paragraphs_grid_get_field_by_type(FieldableEntityInterface $entity, $type) {
  $fields = $entity
    ->getFields();
  foreach ($fields as $field) {
    $field_type = $field
      ->getFieldDefinition()
      ->getType();
    if ($field_type == $type) {
      return $field;
    }
  }
  return NULL;
}