You are here

show_title.module in Show title 8

Same filename and directory in other branches
  1. 2.0.x show_title.module

Module Show Title.

File

show_title.module
View source
<?php

/**
 * @file
 * Module Show Title.
 */
use Drupal\block\Entity\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Template\Attribute;
use Drupal\node\Entity\NodeType;
use Drupal\taxonomy\Entity\Vocabulary;

/**
 * Implements hook_help().
 */
function show_title_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.show_title':
      $text = file_get_contents(dirname(__FILE__) . '/README.md');
      if (!\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_entity_extra_field_info().
 */
function show_title_entity_extra_field_info() {
  $extra = [];

  // Nodes.
  foreach (NodeType::loadMultiple() as $bundle) {
    $extra['node'][$bundle
      ->id()]['display']['show_title'] = [
      'label' => t('Title'),
      'description' => t('Show Title'),
      'weight' => 100,
      'visible' => TRUE,
    ];
  }

  // Taxonomy terms.
  foreach (Vocabulary::loadMultiple() as $vocabulary) {
    $extra['taxonomy_term'][$vocabulary
      ->id()]['display']['show_title'] = [
      'label' => t('Name'),
      'description' => t('Show Name'),
      'weight' => 100,
      'visible' => TRUE,
    ];
  }
  return $extra;
}

/**
 * Implements hook_ENTITY_TYPE_view().
 */
function show_title_node_view(array &$build, EntityInterface $node, EntityViewDisplayInterface $display, $view_mode) {
  $langcode = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();
  if ($display
    ->getComponent('show_title')) {

    // Add attributes for quickedit.
    $attributes = [
      'data-quickedit-field-id' => 'node/' . $node
        ->id() . '/title/' . $langcode . '/' . $view_mode,
      'class' => 'field field--name-title-text field--type-string quickedit-field quickedit-candidate quickedit-editable',
    ];
    $build['show_title'] = [
      '#type' => 'markup',
      '#markup' => '<div' . new Attribute($attributes) . '>' . $node
        ->getTitle() . '</div>',
    ];
  }
}

/**
 * Implements hook_ENTITY_TYPE_view().
 */
function show_title_taxonomy_term_view(array &$build, EntityInterface $taxonomy_term, EntityViewDisplayInterface $display, $view_mode) {
  $langcode = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();
  if ($display
    ->getComponent('show_title')) {

    // Add attributes for quickedit.
    $attributes = [
      'data-quickedit-field-id' => 'taxonomy_term/' . $taxonomy_term
        ->id() . '/name/' . $langcode . '/' . $view_mode,
      'class' => 'field field--name-name-text field--type-string quickedit-field quickedit-candidate quickedit-editable',
    ];
    $build['show_title'] = [
      '#type' => 'markup',
      '#markup' => '<div' . new Attribute($attributes) . '>' . $taxonomy_term
        ->getName() . '</div>',
    ];
  }
}

/**
 * Implements hook_block_access().
 */
function show_title_block_access(Block $block, $operation, AccountInterface $account) {
  if ($operation == 'view' && $block
    ->getPluginId() == 'page_title_block') {
    if (!show_title_block()) {
      return AccessResult::forbidden();
    }
  }
}

/**
 * Define if the block title need to show.
 *
 * @return bool
 *   For show block.
 */
function show_title_block() {
  $show_block_title = TRUE;
  if (\Drupal::service('router.admin_context')
    ->isAdminRoute()) {
    return $show_block_title;
  }
  if (\Drupal::service('path.matcher')
    ->isFrontPage()) {
    $show_block_title = FALSE;
  }
  if (show_title_is_in_display_mode()) {
    $show_block_title = FALSE;
  }
  return $show_block_title;
}

/**
 * Check if the extra field is used.
 *
 * @return bool
 *   Is used.
 */
function show_title_is_in_display_mode() {
  $entities = [];
  foreach (\Drupal::routeMatch()
    ->getParameters() as $param) {
    if ($param instanceof EntityInterface) {
      $entities[] = $param;
    }
  }
  if (!empty($entities)) {
    $entity = $entities[0];
    $display = \Drupal::entityTypeManager()
      ->getStorage('entity_view_display')
      ->load($entity
      ->getEntityTypeId() . '.' . $entity
      ->bundle() . '.default');
    if (!empty($display)) {
      $content = $display
        ->get('content');
      if (!empty($content['show_title'])) {
        return TRUE;
      }
    }
  }
  return FALSE;
}

/**
 * Implements hook_preprocess_HOOK().
 */
function show_title_preprocess_page(&$variables) {
  $variables['show_title'] = FALSE;
  if (!show_title_block()) {
    $variables['show_title'] = TRUE;
  }
}

/**
 * Implements hook_theme_registry_alter().
 */
function show_title_theme_registry_alter(&$theme_registry) {
  $active_theme = \Drupal::theme()
    ->getActiveTheme();

  // Check if file not exist in active theme.
  if (!file_exists($active_theme
    ->getPath() . '/templates/node.html.twig')) {

    // Check if theme has base theme to select the right overrided template.
    // Base theme stable is default core template.
    $base_theme = 'stable';
    if (!empty($active_theme
      ->getExtension()->base_theme)) {
      $base_theme = $active_theme
        ->getExtension()->base_theme;
    }

    // Set the branding block template.
    $module_path = drupal_get_path('module', 'show_title');
    $theme_registry['node']['path'] = implode('/', [
      $module_path,
      'templates',
      $base_theme,
    ]);
  }
}