alert_types-8.x-1.x-dev/alert_types.module

alert_types.module
<?php

use Drupal\Core\Form\FormStateInterface;
use Drupal\alert_types\Form\AlertForm;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityDisplayInterface;
use Drupal\alert_types\AlertTypeBehaviorOperations;

/**
 * @file
 * Contains alert_types.module.
 */

/**
 * Implements hook_theme().
 */
function alert_types_theme() {
  $theme = [];
  $theme['alert_block'] = [
    'render element' => 'elements',
  ];
  $theme['alert'] = [
    'render element' => 'elements',
    'file' => 'alert_types.page.inc',
    'template' => 'alert',
  ];

  return $theme;
}

/**
 * Implements hook_preprocess_alert().
 */
function alert_types_preprocess_alert(&$variables) {
  $alert = $variables['elements']['#alert'];

  // Pass the alert entity to the template.
  $variables['alert'] = $alert;

  // Add attributes. This is necessary for javascript.
  $variables['attributes']['class'][] = 'alert';
  $variables['attributes']['id'] = 'alert--' . $alert->id();

  \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(AlertTypeBehaviorOperations::class)
    ->preprocessAlert($variables);
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function alert_types_theme_suggestions_alert(array $variables) {
  $suggestions = [];
  $entity = $variables['elements']['#alert'];
  $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');

  $suggestions[] = 'alert__' . $sanitized_view_mode;
  $suggestions[] = 'alert__' . $entity->bundle();
  $suggestions[] = 'alert__' . $entity->bundle() . '__' . $sanitized_view_mode;
  $suggestions[] = 'alert__' . $entity->id();
  $suggestions[] = 'alert__' . $entity->id() . '__' . $sanitized_view_mode;
  return $suggestions;
}

/**
 * Implements hook_form_alter().
 *
 * In this case we're doing a form alter for just the dismissable and
 * dismiss timer fields because they are part of the module. When creating your
 * own behavior you can hide any relevant fields with a form alter or do it
 * as part of the form display settings (recommended).
 */
function alert_types_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form_object = $form_state->getFormObject();
  if ($form_object instanceof AlertForm) {
    $pluginManager = \Drupal::service('plugin.manager.alert_type_behavior');
    $entity = $form_object->getEntity();
    $definitions = $pluginManager->getDefinitionsByAlertTypes([$entity->bundle()]);
    $enabled = [];
    foreach ($definitions as $definition) {
      $enabled[] = $definition['id'];
    }

    if (!in_array('dismissable', $enabled)) {
      unset($form['dismissable']);
    }

    if (!in_array('dismiss_timer', $enabled)) {
      unset($form['dismiss_timer']);
    }
  }
}

/**
 * Implements hook_entity_type_view().
 */
function alert_types_alert_view(array &$build, EntityInterface $entity, EntityDisplayInterface $display, $view_mode) {
  \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(AlertTypeBehaviorOperations::class)
    ->entityTypeView($build, $entity, $display, $view_mode);
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc