You are here

moderated_content_bulk_publish.module in moderated content bulk publish 1.0.x

moderated_content_bulk_publish module.

File

moderated_content_bulk_publish.module
View source
<?php

/**
 * @file
 * moderated_content_bulk_publish module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;

/**
 * Implements hook_help().
 */
function moderated_content_bulk_publish_help($route_name, RouteMatchInterface $route_match) {
  if ($route_name === 'help.page.moderated_content_bulk_publish') {
    $readme_file = file_exists(__DIR__ . '/README.md') ? __DIR__ . '/README.md' : __DIR__ . '/README.txt';
    if (!file_exists($readme_file)) {
      return NULL;
    }
    $text = file_get_contents($readme_file);
    if ($text && !\Drupal::moduleHandler()
      ->moduleExists('markdown')) {
      return '<pre>' . $text . '</pre>';
    }
    else {

      // Use the Markdown filter to render the README.
      $filter_manager = \Drupal::service('plugin.manager.filter');
      $settings = \Drupal::configFactory()
        ->get('markdown.settings')
        ->getRawData();
      $config = [
        'settings' => $settings,
      ];
      $filter = $filter_manager
        ->createInstance('markdown', $config);
      return $filter
        ->process($text, 'en');
    }
  }
  return NULL;
}

/**
 * Implements hook_page_attachments().
 *
 * Adds the moderated-content-bulk-publish libraries to each page.
 */
function moderated_content_bulk_publish_page_attachments(array &$page) {
  $page['#attached']['library'][] = 'moderated_content_bulk_publish/moderated-content-bulk-publish';
}

/**
 * Implements hook_theme()
 */
function moderated_content_bulk_publish_theme($existing, $type, $theme, $path) {
  return [
    'moderated_content_bulk_publish' => [
      'variables' => [
        'test_var' => NULL,
      ],
    ],
  ];
}

// Thanks to https://drupal.stackexchange.com/questions/270396/add-language-switcher-on-admin-toolbar

/**
 * Implements hook_toolbar() (Display a language switcher for available languages on admin toolbar if site has more than one language).
 */
function moderated_content_bulk_publish_toolbar() {

  // Get languages
  $current_language = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();
  $languages = \Drupal::languageManager()
    ->getLanguages();

  // Check if Language module is enabled and there is more than one language
  $moduleHandler = \Drupal::service('module_handler');
  if (count($languages) > 1 && $moduleHandler
    ->moduleExists('language')) {

    // Get current route.
    $route = \Drupal::service('path.matcher')
      ->isFrontPage() ? '<front>' : '<current>';

    // Get links.
    $links = [];
    foreach ($languages as $language) {
      $url = new Url($route, [], [
        'language' => $language,
      ]);
      $links[] = [
        '#markup' => Link::fromTextAndUrl($language
          ->getName(), $url)
          ->toString(),
      ];
    }

    // Set cache.
    $items['admin_toolbar_langswitch'] = [
      '#cache' => [
        'contexts' => [
          'languages:language_interface',
          'url',
        ],
      ],
    ];

    // Build toolbar item and tray.
    $items['admin_toolbar_langswitch'] += [
      '#type' => 'toolbar_item',
      '#weight' => 999,
      'tab' => [
        '#type' => 'link',
        '#url' => Url::fromRoute('entity.configurable_language.collection'),
        '#title' => t('Language') . ': ' . strtoupper($current_language),
        '#attributes' => [
          'class' => [
            'toolbar-item-admin-toolbar-langswitch',
          ],
          'title' => t('Admin Toolbar Langswitch'),
        ],
      ],
      'tray' => [
        '#heading' => t('Admin Toolbar Langswitch'),
        'content' => [
          '#theme' => 'item_list',
          '#items' => $links,
          '#attributes' => [
            'class' => [
              'toolbar-menu',
            ],
          ],
        ],
      ],
    ];
    return $items;
  }
}

Functions

Namesort descending Description
moderated_content_bulk_publish_help Implements hook_help().
moderated_content_bulk_publish_page_attachments Implements hook_page_attachments().
moderated_content_bulk_publish_theme Implements hook_theme()
moderated_content_bulk_publish_toolbar Implements hook_toolbar() (Display a language switcher for available languages on admin toolbar if site has more than one language).