toolbar_plus-1.0.x-dev/toolbar_plus.module

toolbar_plus.module
<?php

/*
 * @file
 * Hooks for the Toolbar + module.
 */

use Drupal\Core\Render\Element;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\block_content\Entity\BlockContent;

/**
 * Implements hook_theme_registry_alter().
 */
function toolbar_plus_theme_registry_alter(&$theme_registry) {
  $path = Drupal::service('module_handler')->getModule('toolbar_plus')->getPath();
  $theme_registry['top_bar']['path'] = "$path/templates";
}

/**
 * Implements hook_preprocess_HOOK().
 */
function toolbar_plus_preprocess_navigation(&$variables) {
  Drupal::service('toolbar_plus.ui')->buildToolbar($variables);
}

/**
 * Implements hook_page_top().
 */
function toolbar_plus_page_top(array &$page_top) {
  $ui = \Drupal::service('toolbar_plus.ui');
  $ui->buildTopBar($page_top);
  $ui->buildSidebars($page_top);
}

/**
 * Implements hook_preprocess_HOOK().
 */
function toolbar_plus_preprocess_top_bar(&$variables) {
  \Drupal::service('toolbar_plus.ui')->preprocessTopBar($variables);
}

/**
 * Implements hook_entity_build_defaults_alter().
 */
function toolbar_plus_entity_build_defaults_alter(array &$build, EntityInterface $entity, $view_mode) {
  // Flag that the entity needs a wrapper. This wrapper is used to update the page
  // once an action happens. e.g. adding a new field to the page, switching to
  // a new tool.
  // @see EntityTemplate->onTwigRenderTemplate()
  // @see EditPlusFormTrait->addEmptyField()
  $build['#toolbar_plus_entity'] = [
    'entity_type' => $entity->getEntityTypeId(),
    'entity_id' => edit_plus_entity_identifier($entity),
    'bundle' => $entity->bundle(),
    // Record what the actual view mode was when this item was built.
    'view_mode' => $view_mode,
  ];
}

/**
 * Implements hook_entity_view_alter().
 */
function toolbar_plus_entity_view_alter(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display) {
  if (!\Drupal::currentUser()->hasPermission('use toolbar plus edit mode')) {
    return;
  }
  // @see LoadEditablePage for the AJAX version.
  $state = \Drupal::service('toolbar_plus.ui')->getEditModeState();
  if (\Drupal::moduleHandler()->moduleExists('workspaces') && $state === 'enabled') {
    if (\Drupal::service('workspaces.information')->isEntitySupported($entity)) {
      // Lock editing the entity down to one workspace at a time.
      $constraints = array_values(array_filter($entity->getTypedData()->getConstraints(), function ($constraint) {
        return $constraint instanceof \Drupal\workspaces\Plugin\Validation\Constraint\EntityWorkspaceConflictConstraint;
      }));

      if (!empty($constraints)) {
        $violations = \Drupal::service('typed_data_manager')->getValidator()->validate(
          $entity->getTypedData(),
          $constraints[0]
        );
        if (count($violations)) {
          $message = $violations->get(0)->getMessage()->__toString();
          $wrapper = sprintf('data-toolbar-plus-entity-wrapper="%s::%s::%s"', $entity->getEntityTypeId(), edit_plus_entity_identifier($entity), $entity->bundle());
          $build['#markup'] = "<div $wrapper>$message</div>";
          unset($build['#theme'], $build['#pre_render'], $build['_layout_builder']);
          foreach (Element::children($build) as $key) {
            unset($build[$key]);
          }
        }
      }
    }
  }
}

function toolbar_plus_entity_identifier(EntityInterface $entity) {
  // @todo Can we just use uuid for everything?
  // @todo This is also duplicated as edit_plus_entity_identifier.
  return !$entity instanceof BlockContent ? $entity->id() : $entity->uuid();
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function toolbar_plus_form_node_type_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  \Drupal::service('toolbar_plus.form_alter.node_type_edit')->formAlter($form, $form_state);
}

/**
 * Edit plus get view mode.
 *
 * Falls back to a view mode that exists for this entity.
 * @todo this needs to be consolidated with the other view mode handling.
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   The entity.
 * @param string|NULL $view_mode
 *   The perspective view mode.
 *
 * @return string
 *   The view mode.
 *
 * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
 * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
 */
function _toolbar_plus_get_view_mode(EntityInterface $entity, string $view_mode = NULL): string {
  static $cached_view_modes;
  $original_id = $entity->getEntityTypeId() . "." . $entity->bundle() . "." . $view_mode;

  if (!empty($cached_view_modes[$original_id])) {
    return $cached_view_modes[$original_id];
  }

  $storage = \Drupal::entityTypeManager()->getStorage('entity_view_display');
  $view_display = $storage->load($original_id);
  if (empty($view_display)) {
    $default_id = $entity->getEntityTypeId() . "." . $entity->bundle() . ".default";
    $view_display = $storage->load($default_id);
  }
  $cached_view_modes[$original_id] = !empty($view_display) ? $view_display->getMode() : 'default';
  return $cached_view_modes[$original_id];
}

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

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