Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
novicell/content_hierarchy / modules / content_hierarchy_path / content_hierarchy_path.module
Size: Mime:
<?php

/**
 * @file
 * Contains content_hierarchy_path.module.
 */

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function content_hierarchy_path_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the content_hierarchy_path module.
    case 'help.page.content_hierarchy_path':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Creates url aliases based on content hierarchy') . '</p>';
      return $output;

    default:
  }
}

/**
 * Implements hook_content_hierarchy_entity_types_alter().
 */
function content_hierarchy_path_content_hierarchy_entity_types_alter(&$entity_types) {
  if (isset($entity_types['node'])) {
    $entity_types['node']['token_type'] = 'node';
    $entity_types['node']['token_label_field'] = 'title';
  }
  if (isset($entity_types['media'])) {
    $entity_types['media']['token_type'] = 'media';
    $entity_types['media']['token_label_field'] = 'name';
  }
  if (isset($entity_types['taxonomy_term'])) {
    $entity_types['taxonomy_term']['token_type'] = 'term';
    $entity_types['taxonomy_term']['token_label_field'] = 'name';
  }
}

/**
 * Implements hook_token_info()
 */
function content_hierarchy_path_token_info() {

  $type = [
    'name' => t('Content hierarchy'),
    'description' => t('Tokens related to individual content items.'),
    'needs-data' => 'node',
  ];

  //  $tokens['ancestors'] = [
  //    'name' => t('Hierarchy ancestors'),
  //    'description' => t('Hierarchy ancestors'),
  //  ];

  $tokens['ancestors-joined-path'] = [
    'name' => t('Hierarchy ancestors joined paths'),
    'description' => t('Hierarchy ancestors path joined'),
  ];

  $result = [
    'types' => [],
    'tokens' => [],
  ];

  /** @var \Drupal\content_hierarchy_path\ContentHierarchyPathUpdater $updater */
  $updater = Drupal::service('content_hierarchy_path.updater');
  foreach ($updater->getActiveSupportedEntityTypes() as $entity_type_id => $type_info) {
    $result['types'][$type_info['token_type']] = $type;
    $result['tokens'][$type_info['token_type']] = $tokens;
  }
  return $result;
}

/**
 * Implements hook_tokens().
 */
function content_hierarchy_path_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];

  /** @var \Drupal\content_hierarchy_path\ContentHierarchyPathUpdater $updater */
  $updater = Drupal::service('content_hierarchy_path.updater');
  $active_types = [];
  foreach ($updater->getActiveSupportedEntityTypes() as $entity_type_id => $type_info) {
    $active_types[$type_info['token_type']] = $entity_type_id;
  }

  if (array_key_exists($type, $active_types)) {
    /** @var \Drupal\content_hierarchy\ContentHierarchyStorage $storage */
    $storage = \Drupal::service('content_hierarchy.storage');
    $cleaner = \Drupal::service('pathauto.alias_cleaner');
    foreach ($tokens as $name => $original) {
      // Find the desired token by name.
      switch ($name) {
        case 'ancestors-joined-path':
          /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
          $entity = $data[$type];
          $content = $storage->loadFromEntity($entity);
          if (is_null($content)) {
            break;
          }

          $ancestors = $storage->findAncestors($content);
          $items = [];
          foreach ($ancestors as $content_ancestor) {
            if ($content_ancestor->isExcluded() || $content_ancestor->isRoot()) {
              continue;
            }

            $items[] = $cleaner->cleanString($content_ancestor->label(), $options);
          }

          $replacements[$original] = implode('/', $items);
          break;
      }
    }
  }
  return $replacements;
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function content_hierarchy_path_form_content_hierarchy_settings_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  $form['#submit'][] = 'content_hierarchy_path_form_submit';
}

function content_hierarchy_path_form_submit(array &$form, FormStateInterface $form_state) {
  /** @var \Drupal\content_hierarchy_path\ContentHierarchyPathUpdater $updater */
  $updater = Drupal::service('content_hierarchy_path.updater');
  $updater->updateConfigs();
}

/**
 * Implements hook_content_hierarchy_update().
 */
function content_hierarchy_path_content_hierarchy_update($content_id, $langcode) {
  $queue = \Drupal::queue('content_hierarchy_path_update');
  $queue->createItem(['content_id' => $content_id, 'langcode' => $langcode]);
}