You are here

class NodeTitle in Freelinking 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/freelinking/NodeTitle.php \Drupal\freelinking\Plugin\freelinking\NodeTitle

Node Title freelinking plugin.

Plugin annotation


@Freelinking(
  id = "nodetitle",
  title = @Translation("Node title"),
  weight = -10,
  hidden = false,
  settings = {
    "nodetypes" = {},
    "failover" = "",
  }
)

Hierarchy

Expanded class hierarchy of NodeTitle

2 files declare their use of NodeTitle
NodeTitleFailoverTest.php in tests/src/Unit/Plugin/freelinking/NodeTitleFailoverTest.php
NodeTitleTest.php in tests/src/Unit/Plugin/freelinking/NodeTitleTest.php

File

src/Plugin/freelinking/NodeTitle.php, line 30

Namespace

Drupal\freelinking\Plugin\freelinking
View source
class NodeTitle extends FreelinkingPluginBase implements FreelinkingPluginInterface, ContainerFactoryPluginInterface {

  /**
   * Entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Initialize method.
   *
   * @param array $configuration
   *   Plugin configugration.
   * @param string $plugin_id
   *   Plugin Id.
   * @param mixed $plugin_definition
   *   Plugin definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Entity type manager for getting entity type.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The module handler service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, ModuleHandlerInterface $moduleHandler) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entityTypeManager;
    $this->moduleHandler = $moduleHandler;
  }

  /**
   * {@inheritdoc}
   */
  public function getIndicator() {
    return '/nt$|nodetitle|title/A';
  }

  /**
   * {@inheritdoc}
   */
  public function getTip() {
    return $this
      ->t('Click to view a local node.');
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return parent::defaultConfiguration() + [
      'settings' => [
        'nodetypes' => [],
        'failover' => '',
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $element = [];
    $node_type_options = [];

    // Get the node type entities from storage from Entity Type Manager.
    // \Drupal\Core\Entity\EntityTypeBundleInfo::getAllBundleInfo() offers an
    // alter, but increased load times when not cached. It is debatable which
    // should be used in the long term.
    $node_types = $this->entityTypeManager
      ->getStorage('node_type')
      ->loadMultiple();
    foreach ($node_types as $entity) {
      $node_type_options[$entity
        ->id()] = $entity
        ->label();
    }
    $element['nodetypes'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Only link to nodes belonging to the following content types:'),
      '#description' => $this
        ->t('Lookup by title to will be restricted to this content type or these content types.'),
      '#options' => $node_type_options,
      '#default_value' => isset($this->configuration['settings']['nodetypes']) ? $this->configuration['settings']['nodetypes'] : [],
    ];
    $failover_options = [
      '_none' => $this
        ->t('Do Nothing'),
      'showtext' => $this
        ->t('Show text (remove markup)'),
    ];
    if ($this->moduleHandler
      ->moduleExists('freelinking_prepopulate')) {
      $failover_options['freelinking_prepopulate'] = $this
        ->t('Add a link to create content when user has access');
    }
    if ($this->moduleHandler
      ->moduleExists('search')) {
      $failover_options['search'] = $this
        ->t('Add link to search content');
    }
    $failover_options['error'] = $this
      ->t('Insert an error message');
    $element['failover'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('If suitable content is not found'),
      '#description' => $this
        ->t('What should freelinking do when the page is not found?'),
      '#options' => $failover_options,
      '#default_value' => isset($this->configuration['settings']['failover']) ? $this->configuration['settings']['failover'] : '',
    ];
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function buildLink(array $target) {
    $link = '';
    $failover_option = $this
      ->getConfiguration()['settings']['failover'];
    $result = $this
      ->getQuery($target);
    if ($result && !empty($result)) {
      $nid = array_shift($result);
      $link = [
        '#type' => 'link',
        '#title' => isset($target['text']) ? $target['text'] : $target['dest'],
        '#url' => Url::fromRoute('entity.node.canonical', [
          'node' => $nid,
        ], [
          'language' => $target['language'],
        ]),
        '#attributes' => [
          'title' => isset($target['tooltip']) ? $target['tooltip'] : $this
            ->getTip(),
        ],
      ];
    }
    elseif ($failover_option !== 'error' && $failover_option !== '_none') {
      return [
        'error' => $failover_option,
      ];
    }
    elseif ($failover_option === 'error') {
      $link = [
        '#theme' => 'freelink_error',
        '#plugin' => 'nodetitle',
        '#message' => $this
          ->t('Node title %target does not exist', [
          '%target' => $target['dest'],
        ]),
      ];
    }
    else {
      $link = [
        '#markup' => '[[nodetitle:' . $target['target'] . ']]',
      ];
    }
    return $link;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'), $container
      ->get('module_handler'));
  }

  /**
   * Get the allowed node types from configuration.
   *
   * @return array
   *   An indexed array of node types.
   */
  protected function getAllowedNodeTypes() {
    $node_types = $this->configuration['settings']['nodetypes'];
    return array_reduce($node_types, function (&$result, $item) {
      if ($item) {
        $result[] = $item;
      }
      return $result;
    }, []);
  }

  /**
   * Get the node query builder.
   *
   * @param array $target
   *   The target array to construct the query.
   *
   * @return bool|array
   *   An array of results or FALSE if an error occurred.
   */
  protected function getQuery(array $target) {
    try {
      $query = $this->entityTypeManager
        ->getStorage('node')
        ->getQuery('AND');
      $node_types = $this
        ->getAllowedNodeTypes();
      if (!empty($node_types)) {
        $query
          ->condition('type', $node_types, 'IN');
      }
      return $query
        ->condition('title', $target['dest'])
        ->condition('status', 1)
        ->condition('langcode', $target['language']
        ->getId())
        ->accessCheck()
        ->execute();
    } catch (InvalidPluginDefinitionException $e) {
      return FALSE;
    } catch (PluginNotFoundException $e) {
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getFailoverPluginId() {
    $non_plugins = [
      '_none',
      '',
      'error',
      'showtext',
    ];
    if (!in_array($this->configuration['settings']['failover'], $non_plugins)) {
      return $this->configuration['settings']['failover'];
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FreelinkingPluginBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
FreelinkingPluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration 1
FreelinkingPluginBase::isHidden public function Determine if the plugin is built-in (always on). Overrides FreelinkingPluginInterface::isHidden
FreelinkingPluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
NodeTitle::$entityTypeManager protected property Entity type manager.
NodeTitle::$moduleHandler protected property Module handler.
NodeTitle::buildLink public function Build a link with the plugin. Overrides FreelinkingPluginInterface::buildLink
NodeTitle::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
NodeTitle::defaultConfiguration public function Gets default configuration for this plugin. Overrides FreelinkingPluginBase::defaultConfiguration
NodeTitle::getAllowedNodeTypes protected function Get the allowed node types from configuration.
NodeTitle::getFailoverPluginId public function Get the failover plugin ID (if applicable). Overrides FreelinkingPluginBase::getFailoverPluginId
NodeTitle::getIndicator public function A regular expression string to indicate what to replace for this plugin. Overrides FreelinkingPluginInterface::getIndicator
NodeTitle::getQuery protected function Get the node query builder.
NodeTitle::getTip public function Provides tips for this freelinking plugin. Overrides FreelinkingPluginInterface::getTip
NodeTitle::settingsForm public function Plugin configuration form. Overrides FreelinkingPluginBase::settingsForm
NodeTitle::__construct public function Initialize method. Overrides PluginBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.