spalp-8.x-1.2/spalp.module

spalp.module
<?php

/**
 * @file
 * Contains spalp.module.
 */

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\field\Entity\FieldStorageConfig;

/**
 * Implements hook_help().
 */
function spalp_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the spalp module.
    case 'help.page.spalp':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('This module provides a way for site managers to configure and serve single-page applications as pages in a Drupal site.') . '</p>';
      return $output;

    default:
  }
}

/**
 * Implements hook_theme().
 */
function spalp_theme() {
  return [
    'node__applanding' => [
      'template' => 'node--applanding',
      'base hook' => 'node',
    ],
  ];
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function spalp_theme_suggestions_node_alter(array &$suggestions, array $variables) {
  // Allow modules to provide a specific template for their app ID.
  $node = $variables['elements']['#node'];
  if ($node->getType() == 'applanding') {
    $app_id = $node->get('field_spalp_app_id')
      ->getString();
    $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.',
      '_');
    $suggestions[] = 'node__applanding__' . $app_id;
    $suggestions[] = 'node__applanding__' . $app_id . '__' . $sanitized_view_mode;
  }
}

/**
 * Implements hook_entity_view().
 */
function spalp_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {

  // Are we viewing an applanding node?
  if ($entity->bundle() == 'applanding' && $view_mode == 'full') {
    // Get the app ID.
    $app_id = $entity->get('field_spalp_app_id')->getString();

    // Add the extending module's library to the render array.
    $library_discovery = \Drupal::service('library.discovery');
    $library = $library_discovery->getLibraryByName($app_id, $app_id);

    if ($library) {
      $build['#attached']['library'][] = "$app_id/$app_id";
    }

    // Are we viewing a revision?
    $revision = \Drupal::routeMatch()->getParameter('node_revision');

    // Add a link to the app's config.
    $build['#attached']['html_head_link'][] = \Drupal::service('spalp.core')->getJsonLink($app_id, $revision);
  }
}

/**
 * Set dynamic allowed values for the app ID field.
 *
 * @param \Drupal\field\Entity\FieldStorageConfig $definition
 *   The field definition.
 * @param \Drupal\Core\Entity\ContentEntityInterface|null $entity
 *   The entity being created if applicable.
 * @param bool $cacheable
 *   Boolean indicating if the results are cacheable.
 *
 * @return array
 *   An array of possible key and value options.
 *
 * @see options_allowed_values()
 */
function spalp_get_app_ids(FieldStorageConfig $definition, ContentEntityInterface $entity = NULL, $cacheable) {
  return \Drupal::service('spalp.spalpconfig')->getAppIds();
}

/**
 * Implements hook_modules_installed().
 */
function spalp_modules_installed($modules) {
  /** @var \Drupal\Core\Extension\ModuleExtensionList $module_list_service */
  $module_list_service = \Drupal::service('extension.list.module');
  $module_list = $module_list_service->getList();

  foreach ($modules as $module) {
    // Does the module have a dependency on spalp?
    $dependencies = array_keys($module_list[$module]->requires);
    if (in_array('spalp', $dependencies)) {
      // Invoke the Spalp Core service to create Nodes.
      \Drupal::service('spalp.core')->createNodes($module);
    }
  }
}

/**
 * Implements hook_entity_bundle_field_info_alter().
 */
function spalp_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
  if ($bundle === 'applanding') {
    if (isset($fields['field_spalp_app_id'])) {
      // Validate that the app ID is unique.
      $fields['field_spalp_app_id']->addConstraint('unique_spalp_app_id', []);
    }
  }
}

/**
 * Implements hook_menu_links_discovered_alter().
 */
function spalp_menu_links_discovered_alter(&$links) {

  // Get all the spalp nodes.
  $query = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->getQuery()
    ->accessCheck(FALSE)
    ->condition('type', 'applanding');
  $nids = $query->execute();
  $nodes = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($nids);

  foreach ($nodes as $id => $node) {
    $links['spalp.node.' . $id] = [
      'title' => $node->getTitle(),
      'route_name' => 'entity.node.edit_form',
      'route_parameters' => [
        'node' => $id,
      ],
      'parent' => 'spalp.settings',
    ];
  }
  return $links;
}

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

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