localgov_news-2.4.2/localgov_news.module

localgov_news.module
<?php

/**
 * @file
 * LocalGov Drupal news article module file.
 */

use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\localgov_news\NewsExtraFieldDisplay;
use Drupal\node\NodeInterface;

/**
 * Implements hook_theme().
 */
function localgov_news_theme($existing, $type, $theme, $path) {
  return [
    'node__localgov_news_article__teaser' => [
      'template' => 'node--localgov-news-article--teaser',
      'base hook' => 'node',
    ],
    'node__localgov_news_article__full' => [
      'template' => 'node--localgov-news-article--full',
      'base hook' => 'node',
    ],
    'field__localgov_newsroom_featured' => [
      'template' => 'field--localgov-newsroom-featured',
      'base hook' => 'field',
    ],
  ];
}

/**
 * Implements hook_tokens_alter().
 *
 * Needed to correctly render schema metadata for categories containing commas.
 */
function localgov_news_tokens_alter(&$replacements, $context) {
  // Remove commas in news categories so they are not split.
  if ($context['type'] == 'node') {
    if (isset($replacements['[node:localgov_news_categories:0:entity]'])) {
      $replacements['[node:localgov_news_categories:0:entity]'] = stripslashes(str_replace(',', '', $replacements['[node:localgov_news_categories:0:entity]']));
    }
  }
}

/**
 * Implements hook_entity_extra_field_info().
 */
function localgov_news_entity_extra_field_info() {
  return \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(NewsExtraFieldDisplay::class)
    ->entityExtraFieldInfo();
}

/**
 * Implements hook_ENTITY_TYPE_view().
 */
function localgov_news_node_view(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) {
  return \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(NewsExtraFieldDisplay::class)
    ->nodeView($build, $node, $display, $view_mode);
}

/**
 * Implements hook_form_alter().
 */
function localgov_news_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  return \Drupal::service('class_resolver')
    ->getInstanceFromDefinition(NewsExtraFieldDisplay::class)
    ->formAlter($form, $form_state, $form_id);
}

/**
 * Implements hook_field_widget_complete_form_alter().
 */
function localgov_news_field_widget_complete_form_alter(&$field_widget_complete_form, FormStateInterface $form_state, $context) {
  // If the localgov_newsroom field only has one newsroom to choose, just select
  // it and hide the field.
  $field_definition = $context['items']->getFieldDefinition();
  if ($field_definition->getName() == 'localgov_newsroom' && isset($field_widget_complete_form['widget']['#options'])) {
    $options = $field_widget_complete_form['widget']['#options'];

    // If there are no newsrooms display a warning.
    if (count($options) == 1 && isset($options['_none'])) {
      $create_newsroom_path = 'localgov_newsroom';
      if (\Drupal::moduleHandler()->moduleExists('localgov_microsites_group')) {
        // @phpstan-ignore-next-line Ignore the next line as this is only present on microsites.
        $group = localgov_microsites_group_get_by_context();
        // @phpstan-ignore-next-line Ignore the next line as this is only present on microsites.
        if ($group instanceof GroupInterface) {
          $create_newsroom_path = 'group_node:' . $create_newsroom_path;
        }
      }

      \Drupal::messenger()->addWarning(t('There are no Newsrooms. Please <a href="./@link">create a Newsroom</a> first, before creating this news article.', ['@link' => $create_newsroom_path]));
    }

    unset($options['_none']);
    if (count($options) == 1) {
      $field_widget_complete_form['widget']['#value'] = key($options);
      $field_widget_complete_form['widget']['#type'] = 'value';
    }
  }

  // Restrict the featured articles to articles in the newsroom.
  // A more durable, and transparent, way of doing this will be to add a custom
  // handler.
  // This is presently just restricting the search query and not the field
  // itself.
  if ($field_definition->getName() == 'localgov_newsroom_featured') {
    if ($form_state->getFormObject() instanceof EntityFormInterface) {
      $nid = $form_state->getformObject()->getEntity()->id();
    }
    if (isset($nid)) {
      foreach ($field_widget_complete_form['widget'] as $delta => $element) {
        if (!empty($element['target_id']) && $element['target_id']['#selection_handler'] == 'views') {
          $field_widget_complete_form['widget'][$delta]['target_id']['#selection_settings']['view']['arguments'] = [$nid];
        }
      }
    }
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc