Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
entity_reference_display.module 3.92 KiB
<?php

/**
 * @file
 * Entity Reference Display module.
 */

use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\entity_reference_display\Plugin\Field\FieldFormatter\EntityReferenceRevisionsDisplayFormatter;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function entity_reference_display_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the entity_reference_display module.
    case 'help.page.entity_reference_display':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('This module defines a simple field type for display mode selection for entity
      reference fields. This allows an editor to select how they would like the
      references displayed. </p> <p> <strong>
      "Display mode" field type: </strong> This field allows you to specify a display mode
      for the rendering of entity reference fields. You can configure available
      options to show only certain display modes. The user then selects one from these
      and affects the way your entity reference field renders items.</p> <p>
      <strong>"Selected display mode" field formatter:</strong> This formatter allows you to render
      referenced entities with a selected display mode. The formatter is available
      only for entity reference fields where the base entity contains at least one
      display mode field. When there are more display fields available, you can choose
      one.') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_form_alter().
 */
function entity_reference_display_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  switch ($form_id) {
    // Disable cardinality settings for 1 value limit.
    case 'field_storage_config_edit_form':
      /** @var \Drupal\field\Entity\FieldConfig $field_config */
      $field_config = $form_state->getStorage()['field_config'];
      // Only for display mode field.
      if ($field_config->getType() == 'entity_reference_display') {
        // Disable editing by user.
        $form['cardinality_container']['#disabled'] = TRUE;
        // Remove states settings to disable JS widget.
        unset($form['cardinality_container']['cardinality_number']['#states']);
      }
      break;
  }
}

/**
 * Implements hook_field_widget_info_alter().
 */
function entity_reference_display_field_widget_info_alter(array &$info) {
  // Allow to use the same widgets as list_string field type.
  entity_reference_display_set_available_plugins($info);
}

/**
 * Implements hook_field_formatter_info_alter().
 */
function entity_reference_display_field_formatter_info_alter(array &$info) {
  // Allow to use the same formatters as list_string field type.
  entity_reference_display_set_available_plugins($info);

  // Replace default formatter when revisions are supported.
  if (\Drupal::moduleHandler()->moduleExists('entity_reference_revisions')) {
    $info['entity_reference_display_default']['class'] = EntityReferenceRevisionsDisplayFormatter::class;
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function entity_reference_display_preprocess_field(&$variables) {
  // Only for selected display mode formatter.
  if (isset($variables['element']['#formatter'])
    && $variables['element']['#formatter'] === 'entity_reference_display_default'
  ) {
    // Add display mode class for field wrapper.
    if (!empty($variables['element'][0]['#view_mode'])) {
      $class = 'erd-list--' . $variables['element'][0]['#view_mode'];
      $variables['attributes']['class'][] = Html::getClass($class);
    }
  }
}

/**
 * Set available widgets or formatters for display mode field.
 */
function entity_reference_display_set_available_plugins(array &$info) {
  // Search between all plugins for similar type.
  foreach ($info as &$plugin) {
    if (in_array('list_string', $plugin['field_types'])) {
      $plugin['field_types'][] = 'entity_reference_display';
    }
  }
}