You are here

event.module in Event 8

Same filename and directory in other branches
  1. 5.2 event.module
  2. 5 event.module

Contains event.module.

File

event.module
View source
<?php

/**
 * @file
 * Contains event.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\event\Entity\EventTypeInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;

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

    // Main module help for the event module.
    case 'help.page.event':
      $output = '';
      $output .= '<h3>' . t('About Event') . '</h3>';
      $output .= '<p>' . t('Events are entities used to manage events.') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_theme().
 */
function event_theme() {
  $theme = [];
  $theme['event'] = [
    'render element' => 'elements',
    'file' => 'event.page.inc',
    'template' => 'event',
  ];
  $theme['event_content_add_list'] = [
    'render element' => 'content',
    'variables' => [
      'content' => NULL,
    ],
    'file' => 'event.page.inc',
  ];
  return $theme;
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function event_theme_suggestions_event(array $variables) {
  $suggestions = [];
  $entity = $variables['elements']['#event'];
  $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
  $suggestions[] = 'event__' . $sanitized_view_mode;
  $suggestions[] = 'event__' . $entity
    ->bundle();
  $suggestions[] = 'event__' . $entity
    ->bundle() . '__' . $sanitized_view_mode;
  $suggestions[] = 'event__' . $entity
    ->id();
  $suggestions[] = 'event__' . $entity
    ->id() . '__' . $sanitized_view_mode;
  return $suggestions;
}

/**
 * Adds the default description field to an event type.
 *
 * @param \Drupal\event\Entity\EventTypeInterface $type
 *   An event type object.
 * @param string $label
 *   (optional) The label for the description instance.
 *
 * @return \Drupal\field\Entity\FieldConfig
 *   A description field object.
 */
function event_add_description_field(EventTypeInterface $type, $label = 'Description') {

  // Add or remove the description field, as needed.
  $field_storage = FieldStorageConfig::loadByName('event', 'description');
  $field = FieldConfig::loadByName('event', $type
    ->id(), 'description');
  if (empty($field)) {
    $field = FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => $type
        ->id(),
      'label' => $label,
      'settings' => [
        'display_summary' => TRUE,
      ],
    ]);
    $field
      ->save();

    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');

    // Assign widget settings for the default form mode.
    $display_repository
      ->getFormDisplay('event', $type
      ->id())
      ->setComponent('description', [
      'type' => 'text_textarea_with_summary',
    ])
      ->save();

    // Assign display settings for the 'default' and 'teaser' view modes.
    $display_repository
      ->getViewDisplay('event', $type
      ->id())
      ->setComponent('description', [
      'label' => 'hidden',
      'type' => 'text_default',
    ])
      ->save();

    // The teaser view mode is created by the Standard profile and therefore
    // might not exist.
    $view_modes = \Drupal::service('entity_display.repository')
      ->getViewModes('event');
    if (isset($view_modes['teaser'])) {
      $display_repository
        ->getViewDisplay('event', $type
        ->id(), 'teaser')
        ->setComponent('description', [
        'label' => 'hidden',
        'type' => 'text_summary_or_trimmed',
      ])
        ->save();
    }
  }
  return $field;
}

Functions

Namesort descending Description
event_add_description_field Adds the default description field to an event type.
event_help Implements hook_help().
event_theme Implements hook_theme().
event_theme_suggestions_event Implements hook_theme_suggestions_HOOK().