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 / content_hierarchy.module
Size: Mime:
<?php

/**
 * @file
 * Contains merc_content_hierarchy.module.
 */

use Drupal\Core\Cache\Cache;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function content_hierarchy_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the content_hierarchy module.
    case 'help.page.content_hierarchy':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Provides the functionality to organize nodes in a tree/hierarchy.') . '</p>';
      return $output;

    default:
  }
}

/**
 * Implements hook_locale_translation_projects_alter().
 */
function content_hierarchy_locale_translation_projects_alter(&$projects) {
  $module_handler = \Drupal::service('module_handler');
  $path = $module_handler->getModule('content_hierarchy')->getPath();
  $projects['content_hierarchy']['info']['interface translation server pattern'] = $path.'/translations/%language.po';
}

/**
 * Implements hook_theme().
 */
function content_hierarchy_theme() {
  return [
    'content_hierarchy_options' => [
      'variables' => [
        'langcode' => NULL,
        'content_id' => NULL
      ],
    ],
    'content_hierarchy_modal_item' => [
      'variables' => [
        'id' => NULL,
        'langcode' => NULL,
        'content_id' => NULL,
        'content' => NULL,
        'children' => NULL
      ]
    ]
  ];
}

/**
 * Implements hook_preprocess().
 */
function content_hierarchy_preprocess_content_hierarchy_options(&$variables) {
  $langcode = $variables['langcode'];
  $content_id = $variables['content_id'] ?? NULL;
  /** @var \Drupal\content_hierarchy\ContentHierarchyWidgets $widgets */
  $widgets = \Drupal::service('content_hierarchy.widgets');
  /** @var \Drupal\content_hierarchy\ContentHierarchyStorage $storage */
  $storage = \Drupal::service('content_hierarchy.storage');
  $variables['items'] = $widgets->getRenderableOptions($langcode, $content_id);
  $variables['#cache'] = [
    'tags' => $storage->getListCacheTags($langcode)
  ];
}

/**
 * Implements hook_preprocess().
 */
function content_hierarchy_preprocess_content_hierarchy_modal_item(&$variables) {
  $id = $variables['id'];
  $langcode = $variables['langcode'] ?? \Drupal::languageManager()->getDefaultLanguage()->getId();
  $content_id = $variables['content_id'];
  /** @var \Drupal\content_hierarchy\ContentHierarchyWidgets $widgets */
  $widgets = \Drupal::service('content_hierarchy.widgets');
  /** @var \Drupal\content_hierarchy\ContentHierarchyStorage $storage */
  $storage = \Drupal::service('content_hierarchy.storage');
  if (!empty($variables['content'])) {
    $content = $variables['content'];
  } else {
    $content = $storage->load($id, $langcode);
  }
  $variables['attributes']['id'] = 'hierarchy-' . $content->id();
  $variables['attributes']['class'][] = 'hierarchy-item';

  $variables['item'] = [
    '#title' => $content->label(),
    '#type' => 'link',
    '#url' => Url::fromRoute('content_hierarchy.modal.select', [
      'id' => $content->id(),
      'langcode' => $langcode,
      'content_id' => $content_id ?? 0
    ]),
    '#attributes' => [
      'class' => ['use-ajax', 'item-title'],
    ],
  ];

  $children = $content->getChildren();
  if (!empty($children)) {
    if (empty($variables['children'])) {
      $toggle_class = 'expand';
      $route_name = 'content_hierarchy.modal.open';
    } else {
      $toggle_class = 'collapse';
      $route_name = 'content_hierarchy.modal.close';
    }
    $variables['fold_button'] = [
      '#title' => '',
      '#type' => 'link',
      '#url' => Url::fromRoute($route_name, [
        'id' => $content->id(),
        'langcode' => $langcode,
        'content_id' => $content_id ?? 0
      ]),
      '#attributes' => [
        'class' => ['use-ajax', 'button', 'toggle-item-button', $toggle_class],
      ],
    ];
  }
  $variables['#cache'] = ['max-age' => 0];
}

/**
 * Implements hook_menu_links_discovered_alter().
 */
function content_hierarchy_menu_links_discovered_alter(&$links) {
  $config = Drupal::config('content_hierarchy.hierarchy_settings');

  if ($config->get('override_content_menu_item')) {
    // Change parent of content links to content overview.
    foreach ($links as $id => $link) {
      if (isset($link['parent']) && $link['parent'] == 'system.admin_content') {
        $links[$id]['parent'] = 'content_hierarchy.content_overview';
      }
    }
    // Replace content link with new content link.
    if (isset($links['system.admin_content'])) {
      $links['system.admin_content']['parent'] = 'content_hierarchy.content_overview';
      $links['system.admin_content']['weight'] = 101;
      $links['system.admin_content']['title'] = t('All content');
    }
  } else {
    // Remove override content link.
    unset($links['content_hierarchy.content_overview']);
  }
}

/**
 * Implements hook_menu_local_tasks_alter().
 */
function content_hierarchy_menu_local_tasks_alter(&$data, $route_name, \Drupal\Core\Cache\RefinableCacheableDependencyInterface &$cacheability) {
  if (is_array($data) && isset($data['tabs']) && isset($data['tabs'][0]) && isset($data['tabs'][0]['system.admin_content'])) {
    $data['tabs'][0]['system.admin_content']['#weight'] = 101;
    $data['tabs'][0]['system.admin_content']['#link']['title'] = t('All content');
  }
}

/**
 * Implements hook_page_attachments_alter().
 */
function content_hierarchy_page_attachments_alter(array &$page) {
  $user = \Drupal::currentUser();
  if ($user->hasPermission('access toolbar')) {
    $page['#attached']['library'][] = 'content_hierarchy/content_hierarchy';
  }
}

/**
 * Remove content from hierarchy and invalidate tags on entity delete.
 * @param \Drupal\Core\Entity\EntityInterface $entity
 */
function content_hierarchy_entity_delete(Drupal\Core\Entity\EntityInterface $entity) {
  /** @var \Drupal\content_hierarchy\ContentHierarchyStorage $storage */
  $storage = \Drupal::service('content_hierarchy.storage');
  if ($storage->isEntityInHierarchy($entity)) {
    $storage->deleteEntityFromHierarchy($entity);
  }
}

/**
 * Invalidate tag on node delete.
 * @param \Drupal\Core\Entity\EntityInterface $entity
 */
/**
 * Implements hook_entity_insert().
 */
function content_hierarchy_entity_insert(Drupal\Core\Entity\EntityInterface $entity) {
  /** @var \Drupal\content_hierarchy\ContentHierarchyStorage $storage */
  $storage = \Drupal::service('content_hierarchy.storage');
  if ($storage->isEntityInHierarchy($entity)) {
    Cache::invalidateTags(['content_hierarchy_list:' . $entity->language()->getId()]);
  }
}