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

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

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\site_settings\SiteSettingsLoaderInterface;

/**
 * Implements hook_help().
 */
function dds_content_hierarchy_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the dds_content_hierarchy module.
    case 'help.page.dds_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 dds_content_hierarchy_locale_translation_projects_alter(&$projects) {
  $module_handler = \Drupal::service('module_handler');
  $path = $module_handler->getModule('dds_content_hierarchy')->getPath();
  $projects['dds_content_hierarchy']['info']['interface translation server pattern'] = $path.'/translations/%language.po';
}

/**
 * Implements hook_theme().
 */
function dds_content_hierarchy_theme() {
  return [
    'dds_content_overview' => [
      'variables' => [
        'list' => NULL,
        'title' => '',
      ],
    ],
    'dds_content_list' => [
      'variables' => [
        'items' => [],
        'attributes' => []
      ],
    ],
  ];
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function dds_content_hierarchy_form_node_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  //only administrators should be able to edit the hierarchy of special pages
  if (
    isset($form['field_parent']) &&
    strpos($form_id, 'node_special_page') && !
    \Drupal::currentUser()->hasPermission('administer nodes')
  ) {
      unset($form['field_parent']);
  }
  // We should only do it if the node actually has the parent field.
  if (!empty($form['field_parent'])) {
    // move the field_parent field to the advanced column for better admin UX
    $form['hierarchy'] = [
      '#type' => 'details',
      '#title' => t('Hierarchy'),
      '#group' => 'advanced',
      '#attributes' => [
        'class' => [
          'node-form-options'
        ],
      ],
      '#attached' => [
        'library' => [
          'node/drupal.node'
        ],
      ],
      '#weight' => -10,
      '#open' => TRUE
    ];

    $form['field_parent']['#group'] = 'hierarchy';

    $options = $form['field_parent']['widget'][0]['target_id']['#options'];

    // remove 403 and 404 pages as parent options

    /** @var SiteSettingsLoaderInterface|false $site_settings */
    $site_settings_loader = \Drupal::service('plugin.manager.site_settings_loader')->getActiveLoaderPlugin();
    $site_settings = $site_settings_loader->loadByGroup('site_settings');
    $system_pages = $site_settings['system_pages'] ?? [];
    //todo: add multilingual support
    if (!empty($system_pages)) {
      if(isset($options[$system_pages['field_403']])) {
        unset($options[$system_pages['field_403']]);
      }
      if(isset($options[$system_pages['field_404']])) {
        unset($options[$system_pages['field_404']]);
      }
    }

    if(strpos($form_id, 'node_special_page') === false) {
      // if parent is required, remove the _none option, since it's obsolete
      if(isset($options['_none']) && $form['field_parent']['widget'][0]['target_id']['#required'] == true) {
        unset($options['_none']);
      }

      // use frontpage as default parent option
      if(!empty($system_pages) && empty($form['field_parent']['widget'][0]['target_id']['#default_value'])) {
        $form['field_parent']['widget'][0]['target_id']['#default_value'] = $system_pages['field_frontpage'];
      }

      // use zero as default value for weight
      if(empty($form['field_parent']['widget'][0]['weight']['#default_value'])) {
        $form['field_parent']['widget'][0]['weight']['#default_value'] = 0;
      }
    }
    $form['field_parent']['widget'][0]['target_id']['#options'] = $options;
  }
}

/**
 * Implements hook_menu_links_discovered_alter().
 */
function dds_content_hierarchy_menu_links_discovered_alter(&$links) {
  foreach ($links as $id => $link) {
    if (isset($link['parent']) && $link['parent'] == 'system.admin_content') {
      $links[$id]['parent'] = 'dds_content_hierarchy.content_overview';
    }
  }
  if (isset($links['system.admin_content'])) {
    $links['system.admin_content']['parent'] = 'dds_content_hierarchy.content_overview';
    $links['system.admin_content']['weight'] = 101;
    $links['system.admin_content']['title'] = t('All content');
  }
}

/**
 * Implements hook_menu_local_tasks_alter().
 */
function dds_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 dds_content_hierarchy_page_attachments_alter(array &$page) {
  $user = \Drupal::currentUser();
  if ($user->hasPermission('access toolbar')) {
    $page['#attached']['library'][] = 'dds_content_hierarchy/dds_content_hierarchy';
  }
}