You are here

class NodeRevisionGenerateForm in Node Revision Delete 8

Class NodeRevisionGenerate.

Hierarchy

Expanded class hierarchy of NodeRevisionGenerateForm

1 string reference to 'NodeRevisionGenerateForm'
node_revision_generate.routing.yml in modules/node_revision_generate/node_revision_generate.routing.yml
modules/node_revision_generate/node_revision_generate.routing.yml

File

modules/node_revision_generate/src/Form/NodeRevisionGenerateForm.php, line 15

Namespace

Drupal\node_revision_generate\Form
View source
class NodeRevisionGenerateForm extends FormBase {

  /**
   * Drupal\Core\Entity\EntityTypeManagerInterface definition.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The node revision generate interface.
   *
   * @var \Drupal\node_revision_generate\NodeRevisionGenerateInterface
   */
  protected $nodeRevisionGenerate;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   * @param \Drupal\node_revision_generate\NodeRevisionGenerateInterface $node_revision_generate
   *   The node revision generate.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, NodeRevisionGenerateInterface $node_revision_generate) {
    $this->entityTypeManager = $entity_type_manager;
    $this->nodeRevisionGenerate = $node_revision_generate;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('node_revision_generate'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'node_revision_generate_generate_revisions';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    // Get all Content types.
    $content_types = [];
    $form['bundles'] = [];
    $node_types = $this->entityTypeManager
      ->getStorage('node_type')
      ->loadMultiple();
    foreach ($node_types as $type) {
      $content_type_machine_name = $type
        ->id();

      // If the content type don't have nodes should be disabled.
      if (!$this->nodeRevisionGenerate
        ->existsNodesContentType($content_type_machine_name)) {
        $form['bundles'][$content_type_machine_name]['#disabled'] = TRUE;
        $content_types[$content_type_machine_name] = $this
          ->t('@content_type. There are no nodes.', [
          '@content_type' => $type
            ->label(),
        ]);
      }
      else {
        $content_types[$content_type_machine_name] = $type
          ->label();
      }
    }

    // Sort the content types by content type name.
    asort($content_types);
    $form['bundles'] += [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Content types'),
      '#options' => $content_types,
      '#required' => TRUE,
    ];
    $form['revisions_number'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Revisions number'),
      '#min' => 1,
      '#default_value' => 1,
      '#description' => $this
        ->t('The maximum number of revisions that will be created for each node of the selected content types.'),
      '#required' => TRUE,
    ];
    $form['age'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Revisions age'),
      '#description' => $this
        ->t('The age between each generated revision.'),
      '#required' => TRUE,
    ];
    $form['age']['number'] = [
      '#type' => 'number',
      '#min' => 1,
      '#default_value' => 1,
      '#required' => TRUE,
    ];
    $time_options = [
      '86400' => $this
        ->t('Day'),
      '604800' => $this
        ->t('Week'),
      '2592000' => $this
        ->t('Month'),
    ];
    $form['age']['time'] = [
      '#type' => 'select',
      '#options' => $time_options,
    ];
    $form['description'] = [
      '#type' => 'item',
      '#markup' => $this
        ->t('The first revision will be generated starting from the created date of the last node revision and the last one will not have a date in the future. So, depending on this maybe we will not generate the number of revisions you expect.'),
    ];
    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Generate revisions'),
      '#button_type' => 'primary',
    ];

    // Adding donation text.
    $form['#prefix'] = Donation::getDonationText();
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Get selected content types.
    $bundles = array_filter($form_state
      ->getValue('bundles'));

    // Get form values.
    $revisions_number = $form_state
      ->getValue('revisions_number');
    $interval_number = $form_state
      ->getValue('number');
    $interval_time = $form_state
      ->getValue('time');

    // Get interval to generate revisions.
    $revisions_age = $interval_number * $interval_time;

    // Get the available nodes to generate revisions.
    $nodes_for_revisions = $this->nodeRevisionGenerate
      ->getAvailableNodesForRevisions($bundles, $revisions_age);

    // Check if there is nodes to generate revisions.
    if ($nodes_for_revisions) {

      // Setting the batch.
      batch_set($this->nodeRevisionGenerate
        ->getRevisionCreationBatch($nodes_for_revisions, $revisions_number, $revisions_age));
    }
    else {
      $this
        ->messenger()
        ->addWarning($this
        ->t('There are not more available nodes to generate revisions of the selected content types and specified options.'));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
NodeRevisionGenerateForm::$entityTypeManager protected property Drupal\Core\Entity\EntityTypeManagerInterface definition.
NodeRevisionGenerateForm::$nodeRevisionGenerate protected property The node revision generate interface.
NodeRevisionGenerateForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
NodeRevisionGenerateForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
NodeRevisionGenerateForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
NodeRevisionGenerateForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
NodeRevisionGenerateForm::__construct public function Constructor.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.