wayfinding-2.1.x-dev/wayfinding.module

wayfinding.module
<?php

/**
 * @file
 * Module file for the wayfinding module.
 */

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\digital_signage_framework\Entity\Device;

/**
 * Implements hook_entity_base_field_info().
 */
function wayfinding_entity_base_field_info(EntityTypeInterface $entity_type): array {
  if (!in_array($entity_type->id(), Drupal::service('wayfinding.entity_types')->allDisabledIds(), TRUE)) {
    /** @var \Drupal\wayfinding\EntityUpdate $service */
    $service = Drupal::service('wayfinding.entity_update');
    return [
      'wayfinding' => $service->fieldDefinition(),
    ];
  }
  return [];
}

/**
 * Implements hook_entity_presave().
 */
function wayfinding_entity_presave(EntityInterface $entity): void {
  Drupal::service('wayfinding.events')->presave($entity);
}

/**
 * Implements hook_entity_insert().
 */
function wayfinding_entity_insert(EntityInterface $entity): void {
  Drupal::service('wayfinding.events')->update($entity);
}

/**
 * Implements hook_entity_update().
 */
function wayfinding_entity_update(EntityInterface $entity): void {
  Drupal::service('wayfinding.events')->update($entity);
}

/**
 * Implements hook_entity_delete().
 */
function wayfinding_entity_delete(EntityInterface $entity): void {
  Drupal::service('wayfinding.events')->update($entity);
}

/**
 * Implements hook_form_alter().
 *
 * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
 * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
 */
function wayfinding_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
  if ($form_id === 'media_wayfinding_edit_form') {
    $form['name']['#access'] = FALSE;
    $form['field_perspective']['#access'] = FALSE;
    $form['field_destination']['#access'] = FALSE;
    $form['wayfinding']['#access'] = FALSE;
  }
  elseif ($form_id === 'media_wayfinding_add_form') {
    $form['name']['#access'] = FALSE;
    $form['field_perspective']['#access'] = FALSE;
    $form['field_destination']['#access'] = FALSE;
    $form['wayfinding']['#access'] = FALSE;
    foreach (['name', 'perspective', 'destination_type', 'destination_id'] as $item) {
      if (!isset($_GET[$item])) {
        $form['actions']['#access'] = FALSE;
        Drupal::messenger()->addWarning(t('You should not add wayfinding media directly, go to <a href="@url">Way finding</a> instead', [
          '@url' => Url::fromRoute('wayfinding.collection')->toString(),
        ]));
        return;
      }
    }
    $form['name']['widget'][0]['value']['#default_value'] = $_GET['name'];
    $form['field_perspective']['widget'][0]['value']['#default_value'] = $_GET['perspective'];
    $form['field_destination']['widget'][0]['target_type']['#default_value'] = $_GET['destination_type'];
    $form['field_destination']['widget'][0]['target_id']['#default_value'] = Drupal::entityTypeManager()->getStorage($_GET['destination_type'])->load($_GET['destination_id']);
  }
  elseif (isset($form['wayfinding'])) {
    $config = Drupal::configFactory()->get('wayfinding.settings');
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $form_state->getBuildInfo()['callback_object']->getEntity();
    $id = implode(' ', [$entity->getEntityTypeId(), $entity->bundle()]);
    $isDestination = !empty($config->get('types')['destinations'][$id]);
    $isSource = !empty($config->get('types')['sources'][$entity->getEntityTypeId()]);
    if (!$isDestination && !$isSource) {
      $form['wayfinding']['#access'] = FALSE;
    }
    elseif (isset($form['advanced'])) {
      $form['wayfinding']['widget'][0]['#group'] = 'advanced';
      $form['wayfinding']['widget'][0]['#open'] = FALSE;
      $form['wayfinding']['widget'][0]['#weight'] = -1;
    }
  }
}

/**
 * Implements hook_inline_entity_form_entity_form_alter().
 */
function wayfinding_inline_entity_form_entity_form_alter(array &$entity_form, FormStateInterface $form_state): void {
  if ($entity_form['#entity_type'] === 'wayfinding') {
    $entity_form['label']['#states']['disabled']['input[name="wayfinding[0][inline_entity_form][auto_label][value]"]'] = ['checked' => TRUE];
    foreach (['auto_label', 'label', 'geolocation', 'svgid'] as $item) {
      /* @noinspection UnsupportedStringOffsetOperationsInspection */
      $entity_form[$item]['#states']['invisible']['input[name="wayfinding[0][inline_entity_form][status][value]"]'] = ['checked' => FALSE];
    }
    if ($isDestination = method_exists($form_state->getBuildInfo()['callback_object'], 'getEntity')) {
      $config = Drupal::configFactory()->get('wayfinding.settings');
      /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
      $entity = $form_state->getBuildInfo()['callback_object']->getEntity();
      $id = implode(' ', [$entity->getEntityTypeId(), $entity->bundle()]);
      $isDestination = !empty($config->get('types')['destinations'][$id]);
    }
    if (!$isDestination) {
      $entity_form['geolocation']['#access'] = FALSE;
      $entity_form['svgid']['#access'] = FALSE;
    }
  }
}

/**
 * Implements hook_theme().
 */
function wayfinding_theme(): array {
  return [
    'wayfinding' => [
      'variables' => [
        'view' => NULL,
        'blocks' => [],
        'did' => 0,
        'eid' => '',
      ],
    ],
    'wayfinding_inline' => [
      'variables' => [
        'view' => NULL,
        'blocks' => [],
        'did' => 0,
        'eid' => '',
      ],
    ],
    'views_view_wayfinding_wrapper' => [
      'variables' => [
        'view_array' => [],
        'view' => NULL,
        'rows' => [],
        'header' => [],
        'footer' => [],
        'empty' => [],
        'feed_icons' => [],
        'title' => '',
        'attachment_before' => [],
        'attachment_after' => [],
        'popupdestination' => 'popup',
        'popupcontent' => '',
        'orientation' => 'unknown',
      ],
      'template' => 'views-view-wayfinding-wrapper',
    ],
  ];
}

/**
 * Prepares variables for views wayfinding master templates.
 *
 * Default template: views-view-wayfinding-wrapper.html.twig.
 *
 * @param array $variables
 *   The variables for the twig template.
 */
function wayfinding_preprocess_views_view_wayfinding_wrapper(array &$variables): void {
  /** @var \Drupal\views\ViewExecutable $view */
  $view = $variables['view'];
  if (empty($view->args[1])) {
    $perspective = $view->args[0] ?? 0;
    $variables['bgimg'] = Drupal::service('wayfinding.query')->getRenderedMedia($perspective, 'user', 1, TRUE);
    $variables['pinimg'] = Drupal::service('wayfinding.query')->getRenderedPin();
  }
  else {
    /** @var \Drupal\digital_signage_framework\DeviceInterface $device */
    $device = Device::load($view->args[1]);
    $variables['orientation'] = $device->getOrientation();
  }
  $variables['popupdestination'] = Drupal::service('wayfinding.query')->getPopupDestination();
  $variables['popupcontent'] = Drupal::service('wayfinding.query')->getPopupContent();
  $variables['#attached']['library'][] = 'wayfinding/general';

  // Sort attachments.
  foreach (['before', 'after'] as $type) {
    $attachmentKey = 'attachment_' . $type;
    if (isset($variables[$attachmentKey])) {
      $list = $variables[$attachmentKey];
      $variables['attachment_' . $type] = [];
      foreach ($list as $item) {
        $display = $view->storage->getDisplay($item['#display_id']);
        $variables['attachment_' . $type][$display['position']] = $item;
      }
      ksort($variables['attachment_' . $type]);
    }
  }
  if ($title = Drupal::config('wayfinding.settings')->get('wrapper title')) {
    $variables['title'] = t('@title', ['@title' => $title]);
  }
}

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

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