Commit fdfc7cb0 authored by George's avatar George
Browse files

Issue #3265990 by geoanders: Drupal code standards fixes.

parent 092458bf
Loading
Loading
Loading
Loading
+12 −3
Original line number Diff line number Diff line
## Introduction
## Anti Duplicates

Anti-Duplicates is a module that helps you avoid duplicate content on your website by displaying possible "duplicates" for the content you are posting, based on the keyword on the title of that content.
Anti-Duplicates is a module that helps you avoid duplicate content on
your website by displaying possible "duplicates" for the content you
are posting, based on the keyword on the title of that content.

The module also offers the "Disable form submission" option, which allows the user to disable the form submission, and only re-enable it when agreeing that the content is unique, by clicking on a "not a duplicate" link.
The module also offers the "Disable form submission" option, which
allows the user to disable the form submission, and only re-enable
it when agreeing that the content is unique, by clicking on a
"not a duplicate" link.

### Maintainers

* George Anderson (geoanders) : https://www.drupal.org/u/geoanders
+6 −5
Original line number Diff line number Diff line
@@ -11,15 +11,16 @@ use Drupal\Core\Form\FormStateInterface;
 * Implements hook_form_alter().
 */
function anti_duplicates_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  /**
   * Check if there's a title, since our search of duplicates is based on the
   * provided node title.
   */
  // Check if there's a title, since our search of duplicates is based on the
  // provided node title.
  if (!empty($form['title']) && in_array('node-form', $form['#attributes']['class'])) {

    $parameters = \Drupal::routeMatch()->getParameters();
    if ($parameters->has('node_type')) {

      $config = \Drupal::config('anti_duplicates.settings');
      $content_types = array_filter($config->get('anti_duplicates_content_types') ? $config->get('anti_duplicates_content_types') : []);

      /** @var \Drupal\node\Entity\NodeType $node_type */
      $node_type = $parameters->get('node_type');

@@ -42,7 +43,7 @@ function anti_duplicates_form_alter(&$form, FormStateInterface $form_state, $for

        // Add an Ajax callback to the title field.
        $form['title']['widget'][0]['value']['#ajax'] = [
          'callback' => 'Drupal\anti_duplicates\Form\FormAlter::duplicatesSearch',
          'callback' => 'Drupal\anti_duplicates\Form\AntiDuplicatesFormAlter::duplicatesSearch',
          'event' => 'change',
          'wrapper' => 'dw_container',
          'progress' => [
+2 −0
Original line number Diff line number Diff line
@@ -7,3 +7,5 @@ anti_duplicates.admin_page:
  parent: system.admin_config_content
  requirements:
    _permission: 'administer anti_duplicates'
  options:
    _admin_route: TRUE
+52 −9
Original line number Diff line number Diff line
@@ -2,14 +2,52 @@

namespace Drupal\anti_duplicates\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Manages the Coming Soon admin form.
 * Anti duplicates admin form.
 */
class AntiDuplicatesAdminForm extends ConfigFormBase {

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

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   Config factory object.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Entity type manager service object.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entityTypeManager) {
    parent::__construct($config_factory);
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * Service injection.
   *
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   *   Container object.
   *
   * @return static
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory'),
      $container->get('entity_type.manager')
    );
  }

  /**
   * {@inheritdoc}
   */
@@ -30,7 +68,6 @@ class AntiDuplicatesAdminForm extends ConfigFormBase {
  public function buildForm(array $form, FormStateInterface $form_state) {

    $form = parent::buildForm($form, $form_state);

    $config = $this->config('anti_duplicates.settings');

    $form['anti_duplicates_message'] = [
@@ -53,16 +90,16 @@ class AntiDuplicatesAdminForm extends ConfigFormBase {
      '#title' => $this->t('Search type'),
      '#default_value' => $config->get('anti_duplicates_search_type'),
      '#options' => [
        0 => 'Contains a sequence of the title keywords',
        1 => 'Contains the exact title',
        2 => 'Contains any word from the title',
        0 => $this->t('Contains a sequence of the title keywords'),
        1 => $this->t('Contains the exact title'),
        2 => $this->t('Contains any word from the title'),
      ],
      '#required' => TRUE,
      '#description' => $this->t("Choose the way Anti-Duplicates search for possible duplicates."),
    ];

    $types = [];
    $content_types = \Drupal::service('entity_type.manager')->getStorage('node_type')->loadMultiple();
    $content_types = $this->entityTypeManager->getStorage('node_type')->loadMultiple();
    foreach ($content_types as $content_type) {
      $types[$content_type->id()] = $content_type->label();
    }
@@ -70,7 +107,10 @@ class AntiDuplicatesAdminForm extends ConfigFormBase {
      '#type' => 'checkboxes',
      '#options' => $types,
      '#title' => $this->t('Content types'),
      '#default_value' => !empty($config->get('anti_duplicates_content_types')) ? $config->get('anti_duplicates_content_types') : ['article', 'page'],
      '#default_value' => !empty($config->get('anti_duplicates_content_types')) ? $config->get('anti_duplicates_content_types') : [
        'article',
        'page',
      ],
      '#description' => $this->t('Select content types you want to use the anti duplicates for. If none are selected, no restriction based on types will be implemented.'),
    ];

@@ -83,6 +123,7 @@ class AntiDuplicatesAdminForm extends ConfigFormBase {
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Get submitted values.
    $values = $form_state->getValues();

    // Save the configuration.
    $this->config('anti_duplicates.settings')
      ->set('anti_duplicates_message', $values['anti_duplicates_message'])
@@ -90,8 +131,10 @@ class AntiDuplicatesAdminForm extends ConfigFormBase {
      ->set('anti_duplicates_search_type', $values['anti_duplicates_search_type'])
      ->set('anti_duplicates_content_types', is_array($values['anti_duplicates_content_types']) ? $values['anti_duplicates_content_types'] : [])
      ->save();

    // Display success message.
    $this->messenger()->addMessage($this->t('Anti Duplicates configuration submitted successfully.'), 'status', TRUE);
    $this->messenger()
      ->addMessage($this->t('Anti Duplicates configuration submitted successfully.'), 'status', TRUE);
  }

}
+27 −20
Original line number Diff line number Diff line
@@ -10,9 +10,9 @@ use Drupal\Core\Ajax\RemoveCommand;
use Drupal\Core\Url;

/**
 * Hooks all node forms.
 * Hooks into all node forms.
 */
class FormAlter {
class AntiDuplicatesFormAlter {

  /**
   * Search for duplicates based on the title of the node being created.
@@ -30,32 +30,38 @@ class FormAlter {

    // Get the search type.
    switch ($config->get('anti_duplicates_search_type')) {
      // Contains a sequence of the title keywords.
      case 0:
        $count = self::getNodesDefaultSearchType($title, $nodes);
        break;

      // Contains the exact title.
      case 1:
        $title = '%' . $title . '%';
        $nids = \Drupal::entityQuery('node')->condition('title', $title, 'LIKE')->range(0, 5)->execute();
        $nids = \Drupal::entityQuery('node')
          ->condition('title', $title, 'LIKE')
          ->range(0, 5)
          ->execute();
        $nodes = Node::loadMultiple($nids);
        $count = \Drupal::entityQuery('node')->condition('title', $title, 'LIKE')->count()->execute();
        $count = \Drupal::entityQuery('node')
          ->condition('title', $title, 'LIKE')
          ->count()
          ->execute();
        break;

      // Contains any word from the title.
      case 2:
        $title_array = explode(' ', $title);
        $nids_query = \Drupal::entityQuery('node');
        $orGroup = $nids_query->orConditionGroup();
        foreach ($title_array as $key => $value) {
        foreach ($title_array as $value) {
          $orGroup->condition('title', '%' . $value . '%', 'LIKE');
        }

        $nids = $nids_query->condition($orGroup)->range(0, 5)->execute();
        $nodes = Node::loadMultiple($nids);
        $count = \Drupal::entityQuery('node')->condition($orGroup)->count()->execute();
        break;

      default:
        $nodes = self::get_nodes_default_search_type($title);
        $count = \Drupal::entityQuery('node')
          ->condition($orGroup)
          ->count()
          ->execute();
        break;
    }

@@ -74,15 +80,13 @@ class FormAlter {
    foreach ($nodes as $nid => $node) {
      $options = ['absolute' => TRUE];
      $url = Url::fromRoute('entity.node.canonical', ['node' => $nid], $options);
      $url = '<a href="' . $url->toString() . '" title="' . $node->title->value . '" target="_blank">' . $node->title->value . '</a>';
      $url = '<a href="' . $url->toString() . '" title="' . $node->get('title')->value . '" target="_blank">' . $node->get('title')->value . '</a>';
      $response->addCommand(new AppendCommand('#dw-listing', '<li>' . $url . '</li>'));
    }

    /*
    add a link to the anti-duplicates form when the option disable form is
    checked, this link allows the user to enable the form submission by
    agreeing that the content he is writing is unique.
     */
    // Add a link to the anti-duplicates form when the option disable form is
    // checked, this link allows the user to enable the form submission by
    // agreeing that the content is unique.
    if ($config->get('anti_duplicates_form_submission') && intval($count) > 0) {
      $response->addCommand(new RemoveCommand('#dw-enable-form'));
      $btn = '<a href="#" class="button" id="dw-enable-form">' . t('not a duplicate') . '</a>';
@@ -99,13 +103,16 @@ class FormAlter {
  /**
   * Get nodes for the default search mode.
   */
  private function getNodesDefaultSearchType($the_title, &$nodes) {
  protected static function getNodesDefaultSearchType($the_title, &$nodes) {
    $title = '%' . str_replace(' ', '%', $the_title) . '%';
    $query = \Drupal::entityQuery('node')->condition('title', $title, 'LIKE');
    $nids = $query->range(0, 5)->execute();
    $nodes = Node::loadMultiple($nids);

    return \Drupal::entityQuery('node')->condition('title', $title, 'LIKE')->count()->execute();
    return \Drupal::entityQuery('node')
      ->condition('title', $title, 'LIKE')
      ->count()
      ->execute();
  }

}
Loading