Commit 4b92072c authored by Guido Robertone's avatar Guido Robertone Committed by Adrian Cid Almaguer
Browse files

Issue #2911851: Drush command to configure the tracked content types

parent 0b5cca98
Loading
Loading
Loading
Loading
+65 −0
Changes for src/Commands/NodeRevisionDeleteCommands.php: 65 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\node_revision_delete\Commands;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\node\Entity\NodeType;
use Drupal\node_revision_delete\NodeRevisionDeleteInterface;
use Drush\Commands\DrushCommands;
use Consolidation\AnnotatedCommand\CommandData;
@@ -514,4 +515,68 @@ class NodeRevisionDeleteCommands extends DrushCommands {
    return TRUE;
  }

  /**
   * Track a content type into node revision delete system.
   *
   * @usage nrd-track article 50 1 15
   *   Track the article content type with:
   *   50 minimum number of revision to keep.
   *   1 minimum age of revision to delete.
   *   15 when to delete the revisions.
   *
   * @command nrd:track
   * @aliases nrd-t, nrd-track
   */
  public function track(string $content_type, int $minimum_revisions_to_keep, int $minimum_age_to_delete, int $when_to_delete) {
    // Content_type argument must be string.
    if (!is_string($content_type)) {
      $this->io()->error(dt('Argument content type must be string.'));
      return FALSE;
    }

    // Validate for a valid content type.
    $content_types = $this->entityTypeManager->getStorage('node_type')->loadMultiple();
    $content_types_ids = array_map(function(NodeType $object) { return $object->id(); }, $content_types);
    if (!in_array($content_type, $content_types_ids)) {
      $this->io()->error(dt('Argument content type is not a valid content type.'));
      return FALSE;
    }

    // minimum_revisions_to_keep argument must be numeric.
    if (!is_int($minimum_revisions_to_keep)) {
      $this->io()->error(dt('Argument minimum_revisions_to_keep must be numeric.'));
      return FALSE;
    }

    // minimum_age_to_delete argument must be numeric.
    if (!is_int($minimum_age_to_delete)) {
      $this->io()->error(dt('Argument minimum_age_to_delete must be numeric.'));
      return FALSE;
    }

    // when_to_delete argument must be numeric.
    if (!is_int($when_to_delete)) {
      $this->io()->error(dt('Argument when_to_delete must be numeric.'));
      return FALSE;
    }

    // Validate for Maximum number allowed.
    $config = $this->configFactory->getEditable('node_revision_delete.settings');
    $node_revision_delete_minimum_age_to_delete_time = $config->get('node_revision_delete_minimum_age_to_delete_time');
    $node_revision_delete_when_to_delete_time = $config->get('node_revision_delete_when_to_delete_time');
    if ($minimum_age_to_delete > $node_revision_delete_minimum_age_to_delete_time['max_number']) {
      $this->io()->error(dt('Argument minimum_age_to_delete must lower than Maximum Age of revision to delete.'));
      return FALSE;
    }

    if ($when_to_delete > $node_revision_delete_when_to_delete_time['max_number']) {
      $this->io()->error(dt('Argument when_to_delete must lower than Maximum of When to Delete.'));
      return FALSE;
    }

    $this->nodeRevisionDelete->saveContentTypeConfig($content_type, $minimum_revisions_to_keep, $minimum_age_to_delete, $when_to_delete);

    return TRUE;
  }

}