Commit dbcb0e5c authored by Adrian Cid Almaguer's avatar Adrian Cid Almaguer Committed by Adrian Cid Almaguer
Browse files

Issue #3281621 by adriancid: Drush command to untrack content types

parent 35c725b2
Loading
Loading
Loading
Loading
+69 −14
Changes for src/Commands/NodeRevisionDeleteCommands.php: 69 added lines, 14 removed lines.
Original line number Diff line number Diff line
@@ -380,7 +380,7 @@ class NodeRevisionDeleteCommands extends DrushCommands {
  }

  /**
   * Validate inputs before executing the drush command.
   * Validate inputs before executing the drush command node-revision-delete.
   *
   * @param \Consolidation\AnnotatedCommand\CommandData $commandData
   *   The command data.
@@ -394,18 +394,7 @@ class NodeRevisionDeleteCommands extends DrushCommands {
    $input = $commandData->input();
    $type = $input->getArgument('type');

    // Make sure the content type exists and is configured.
    $available_content_types = array_map(function ($content_type) {
      /** @var \Drupal\node\NodeTypeInterface $content_type */
      return $content_type->id();
    }, $this->nodeRevisionDelete->getConfiguredContentTypes());

    if (!in_array($type, $available_content_types)) {
      $this->io()->error(dt('The following content type is not configured for revision deletion: @name',
        [
          '@name' => $type,
        ]
      ));
    if (!$this->configuredContentType($type)) {
      return FALSE;
    }

@@ -419,7 +408,7 @@ class NodeRevisionDeleteCommands extends DrushCommands {
  }

  /**
   * Validate inputs before executing the drush command.
   * Validate inputs before executing the drush command nrd-dpr.
   *
   * @param \Consolidation\AnnotatedCommand\CommandData $commandData
   *   The command data.
@@ -459,4 +448,70 @@ class NodeRevisionDeleteCommands extends DrushCommands {
    return TRUE;
  }

  /**
   * Untrack a content type.
   *
   * @param string $type
   *   The content type name.
   *
   * @usage nrd-untrack article
   *   Untrack the content type article.
   * @command nrd:untrack
   * @aliases nrd-u, nrd-untrack
   */
  public function untrack(string $type): void {
    $this->nodeRevisionDelete->deleteContentTypeConfig($type);
    $message = dt('<info>The content type @type is now untracked.</info>', ['@type' => $type]);
    $this->writeln($message);
  }

  /**
   * Validate inputs before executing the drush command nrd-untrack.
   *
   * @param \Consolidation\AnnotatedCommand\CommandData $commandData
   *   The command data.
   *
   * @return bool
   *   Returns TRUE if the validations has passed FALSE otherwise.
   *
   * @hook validate nrd-u
   */
  public function untrackValidate(CommandData $commandData): bool {
    $input = $commandData->input();
    $type = $input->getArgument('type');

    if (!$this->configuredContentType($type)) {
      return FALSE;
    }

    return TRUE;
  }

  /**
   * Validate inputs before executing the drush command nrd-untrack.
   *
   * @param string $type
   *   The content type name.
   *
   * @return bool
   *   Returns if a content type is configured.
   */
  private function configuredContentType(string $type): bool {
    // Make sure the content type exists and is configured.
    $available_content_types = array_map(static function ($content_type) {
      /** @var \Drupal\node\NodeTypeInterface $content_type */
      return $content_type->id();
    }, $this->nodeRevisionDelete->getConfiguredContentTypes());

    if (!in_array($type, $available_content_types, TRUE)) {
      $this->io()->error(dt('The following content type is not configured for revision deletion: @name',
        [
          '@name' => $type,
        ]
      ));
      return FALSE;
    }
    return TRUE;
  }

}