class NodeTitle in Freelinking 4.0.x
Same name and namespace in other branches
- 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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\freelinking\Plugin\FreelinkingPluginBase implements FreelinkingPluginInterface
- class \Drupal\freelinking\Plugin\freelinking\NodeTitle implements ContainerFactoryPluginInterface, FreelinkingPluginInterface
- class \Drupal\freelinking\Plugin\FreelinkingPluginBase implements FreelinkingPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
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\freelinkingView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FreelinkingPluginBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
FreelinkingPluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
1 |
FreelinkingPluginBase:: |
public | function |
Determine if the plugin is built-in (always on). Overrides FreelinkingPluginInterface:: |
|
FreelinkingPluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
NodeTitle:: |
protected | property | Entity type manager. | |
NodeTitle:: |
protected | property | Module handler. | |
NodeTitle:: |
public | function |
Build a link with the plugin. Overrides FreelinkingPluginInterface:: |
|
NodeTitle:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
NodeTitle:: |
public | function |
Gets default configuration for this plugin. Overrides FreelinkingPluginBase:: |
|
NodeTitle:: |
protected | function | Get the allowed node types from configuration. | |
NodeTitle:: |
public | function |
Get the failover plugin ID (if applicable). Overrides FreelinkingPluginBase:: |
|
NodeTitle:: |
public | function |
A regular expression string to indicate what to replace for this plugin. Overrides FreelinkingPluginInterface:: |
|
NodeTitle:: |
protected | function | Get the node query builder. | |
NodeTitle:: |
public | function |
Provides tips for this freelinking plugin. Overrides FreelinkingPluginInterface:: |
|
NodeTitle:: |
public | function |
Plugin configuration form. Overrides FreelinkingPluginBase:: |
|
NodeTitle:: |
public | function |
Initialize method. Overrides PluginBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |