Skip to content
Snippets Groups Projects
Commit 51f62179 authored by Alvaro Hurtado's avatar Alvaro Hurtado
Browse files

Issue #3417451: Use dependency injection in SearchInTextFields plugin

parent eb03b1d3
Branches
Tags 1.0.0
1 merge request!14Issue #3417451: Use dependency injection in SearchInTextFields plugin
Pipeline #108354 passed
......@@ -5,8 +5,8 @@
* Contains the autotagger.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\NodeInterface;
/**
......
......@@ -3,13 +3,19 @@
namespace Drupal\autotagger_search_in_text\Plugin\Autotagger;
use Drupal\autotagger\AutotaggerPluginBase;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeInterface;
use Drupal\node\NodeTypeInterface;
use Drupal\taxonomy\TermInterface;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the autotagger.
......@@ -21,9 +27,34 @@ use Drupal\Core\StringTranslation\StringTranslationTrait;
* configurable = false
* )
*/
class SearchInTextFields extends AutotaggerPluginBase {
final class SearchInTextFields extends AutotaggerPluginBase implements ContainerFactoryPluginInterface {
use StringTranslationTrait;
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected EntityFieldManagerInterface $entityFieldManager;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected EntityTypeManagerInterface $entityTypeManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = new self($configuration, $plugin_id, $plugin_definition);
$instance->entityFieldManager = $container->get('entity_field.manager');
$instance->entityTypeManager = $container->get("entity_type.manager");
return $instance;
}
/**
* {@inheritdoc}
*/
......@@ -32,7 +63,6 @@ class SearchInTextFields extends AutotaggerPluginBase {
return;
}
$entityManager = \Drupal::service('entity_field.manager');
$source_fields = [];
$destination_fields = [];
......@@ -46,7 +76,7 @@ class SearchInTextFields extends AutotaggerPluginBase {
$type = $form['type']['#default_value'];
$fields = $entityManager->getFieldDefinitions('node', $type);
$fields = $this->entityFieldManager->getFieldDefinitions('node', $type);
/** @var \Drupal\field\Entity\FieldConfig $field */
foreach ($fields as $field) {
......@@ -98,15 +128,6 @@ class SearchInTextFields extends AutotaggerPluginBase {
'#access' => TRUE,
];
$form['autotagger']['autotagger_rebuild_on_submit'] = [
'#type' => 'checkboxes',
'#title' => t('Assign tags on save.'),
'#default_value' => $default_values['autotagger_rebuild_on_submit'] ?? [],
'#options' => ['rebuild' => $this->t('Rebuild on this form submission')],
'#access' => TRUE,
];
$form['actions']['submit']['#submit'][] = [$this, 'autotaggerFormSubmitAutotagNodes'];
$form['#entity_builders'][] = [$this, 'entityBuilder'];
}
......@@ -115,8 +136,15 @@ class SearchInTextFields extends AutotaggerPluginBase {
*/
public function entityPresave(NodeInterface $node) {
// Determine if this node is set to tag only on node creation.
$node_type = NodeType::load($node->bundle());
$settings = $node_type->getThirdPartySetting('autotagger', 'search_in_text_fields');
$storage = $this->entityTypeManager->getStorage($node->getEntityType()->getBundleEntityType());
$entity_type = $storage->load($node->bundle());
if (!$entity_type instanceof ConfigEntityInterface) {
return;
}
$settings = $entity_type->getThirdPartySetting('autotagger', 'search_in_text_fields');
$create_only = $settings['autotagger_tag_on_create_only'];
if ($node->isNew() || !$create_only) {
......@@ -134,58 +162,15 @@ class SearchInTextFields extends AutotaggerPluginBase {
'autotagger_source_field' => $form_state->getValue('autotagger_source_field'),
'autotagger_destination_field' => $form_state->getValue('autotagger_destination_field'),
'autotagger_tag_on_create_only' => $form_state->getValue('autotagger_tag_on_create_only'),
'autotagger_rebuild_on_submit' => $form_state->getValue('autotagger_rebuild_on_submit'),
];
$node_type->setThirdPartySetting('autotagger', 'search_in_text_fields', $autotagger);
}
/**
* Autotag all nodes of certain content type on configuration submit.
*
* @param array $form
* The sended form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The sended form_state.
*/
public function autotaggerFormSubmitAutotagNodes(array $form, FormStateInterface $form_state) {
$type = $form_state->getValue('type');
if ($form_state->getValue('autotagger_rebuild_on_submit')['rebuild'] === 'rebuild') {
$batch = $this->autotaggerGetBatchTagAllNodes($type);
batch_set($batch);
}
}
/**
* Return the batch to autotag all nodes of certain bundle.
*
* @param string $bundle
* The bundle to retag.
*
* @return array
* The batch to execute.
*/
protected function autotaggerGetBatchTagAllNodes($bundle) {
$nids = \Drupal::entityQuery('node')
->condition('type', $bundle)
->accessCheck(FALSE)
->execute();
$batch = [
'title' => t('Autotagging all nodes of type %type', ['%type' => $bundle]),
'operations' => [],
];
foreach ($nids as $nid) {
$batch['operations'][] = [[$this, 'loadAndRetagNode'], [$nid]];
}
return $batch;
}
/**
* Get all the source field types that might be used in configuration.
*
* @return string[]
* The list of source field types.
* The list of source field types.
*/
protected function getSourceFieldsCandidates() {
return [
......@@ -199,25 +184,12 @@ class SearchInTextFields extends AutotaggerPluginBase {
}
/**
* Loads a node and autotags it.
* Tags a node.
*
* @param int $nid
* The id of the node to load.
* @param array $context
* Batch API parameter.
* @param \Drupal\node\NodeInterface $node
* The node to tag.
*/
public function loadAndRetagNode($nid, array &$context) {
$node = \Drupal::service('entity_type.manager')
->getStorage('node')
->load($nid);
$this->tagNode($node);
$node->save();
$context['results'][] = 1;
$context['message'] = 'Node tagged';
}
protected function tagNode (NodeInterface &$node) {
protected function tagNode(NodeInterface &$node): void {
$node_type = NodeType::load($node->bundle());
$autotag_values = $node_type->getThirdPartySetting('autotagger', 'search_in_text_fields');
......@@ -259,7 +231,7 @@ class SearchInTextFields extends AutotaggerPluginBase {
}
}
}
elseif ( in_array($definition->getType(), $this->getSourceFieldsCandidates())) {
elseif (in_array($definition->getType(), $this->getSourceFieldsCandidates())) {
// For text fields, get the value(s).
if (!empty($node->{$field})) {
$this->processTextField($node->{$field}, $text_array);
......@@ -279,11 +251,14 @@ class SearchInTextFields extends AutotaggerPluginBase {
}
// Get the settings for the destination field.
$settings = $node->getFieldDefinition($destination)->getItemDefinition()->getSettings();
// Term references can either use the "default" handler, which assigns a list
// of vocabularies to reference or a "views" handler which uses Views to
// make the list of terms. These need to be handled differently.
$settings = $node
->getFieldDefinition($destination)
->getItemDefinition()
->getSettings();
// Term references can either use the "default" handler, which assigns a
// list of vocabularies to reference or a "views" handler which uses Views
// to make the list of terms. These need to be handled differently.
if ($settings['handler'] == 'views') {
// Pull the possible terms out of the view results.
$name = $settings['handler_settings']['view']['view_name'];
......@@ -306,8 +281,7 @@ class SearchInTextFields extends AutotaggerPluginBase {
->getItemDefinition()
->getSettings()['handler_settings']['target_bundles'];
foreach ($vocabularies as $vocabulary) {
$tree = \Drupal::getContainer()
->get('entity_type.manager')
$tree = $this->entityTypeManager
->getStorage('taxonomy_term')
->loadByProperties(['vid' => $vocabulary]);
......@@ -420,4 +394,5 @@ class SearchInTextFields extends AutotaggerPluginBase {
return strpos($haystack, $needle) !== FALSE;
}
}
......@@ -39,7 +39,7 @@ class Autotagger extends Plugin {
/**
* Is this plugin configurable by bundle.
*
* @var boolean
* @var bool
*/
public $configurable;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment