You are here

class NodeRevisionsByBundleForm in Node Revisions Autoclean 8

Class NodeRevisionsByBundleForm.

Hierarchy

Expanded class hierarchy of NodeRevisionsByBundleForm

1 string reference to 'NodeRevisionsByBundleForm'
node_revisions_autoclean.routing.yml in ./node_revisions_autoclean.routing.yml
node_revisions_autoclean.routing.yml

File

src/Form/NodeRevisionsByBundleForm.php, line 14

Namespace

Drupal\node_revisions_autoclean\Form
View source
class NodeRevisionsByBundleForm extends FormBase {

  /**
   * EntityTypeBundleInfo.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfo
   */
  protected $entityTypeBundleInfo;

  /**
   * ConfigFactory.
   *
   * @var Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * NodeRevisionsByBundleForm constructor.
   *
   * @param Drupal\Core\Entity\EntityTypeBundleInfo $entityTypeBundleInfo
   *   EntityTypeBundleInfo.
   * @param Drupal\Core\Config\ConfigFactory $configFactory
   *   ConfigFactory.
   */
  public function __construct(EntityTypeBundleInfo $entityTypeBundleInfo, ConfigFactory $configFactory) {
    $this->entityTypeBundleInfo = $entityTypeBundleInfo;
    $this->configFactory = $configFactory;
  }

  /**
   * Creates.
   *
   * @param Symfony\Component\DependencyInjection\ContainerInterface $container
   *   ContainerInterface.
   *
   * @return static
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.bundle.info'), $container
      ->get('config.factory'));
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $types = $this->entityTypeBundleInfo
      ->getBundleInfo('node');
    $config = $this->configFactory
      ->get('node_revisions_autoclean.settings');
    $form['enable_on_cron'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable old revisions deletion during cronjobs'),
      '#return_value' => '1',
      '#default_value' => $config
        ->get('enable_on_cron') ? $config
        ->get('enable_on_cron') : '0',
      '#description' => $this
        ->t('Cronjobs will delete old revisions according your parameters.'),
    ];
    $form['enable_on_node_update'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable old revisions deletion on node update'),
      '#description' => $this
        ->t("Each node's revisions will be autoclean on node update"),
      '#return_value' => '1',
      '#default_value' => $config
        ->get('enable_on_node_update') ? $config
        ->get('enable_on_node_update') : '0',
    ];
    $form['explain'] = [
      '#markup' => '<p><i>' . $this
        ->t('You can select none of the above if you wish to delete old revisions using a drush command (drush nra:dor).') . '</i></p>',
    ];
    foreach ($types as $machine_name => $arr) {
      $form['fs_' . $machine_name] = [
        '#type' => 'fieldset',
        '#title' => $this
          ->t('Content type : @content_type', [
          '@content_type' => $arr['label'],
        ]),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
      ];
      $form['fs_' . $machine_name]["node__{$machine_name}"] = [
        '#type' => 'number',
        '#title' => $this
          ->t('Limit revisions for node type @label', [
          '@label' => $arr['label'],
        ]),
        '#description' => $this
          ->t('Max revisions for @label type, "-1" means unlimited number of revisions, "0" keeps only the last.', [
          '@label' => $arr['label'],
        ]),
        '#default_value' => $config
          ->get("node.{$machine_name}") ? $config
          ->get("node.{$machine_name}") : -1,
        '#required' => TRUE,
      ];
      $val = $config
        ->get("interval.{$machine_name}");
      $form['fs_' . $machine_name]['node_enable_date_' . $machine_name] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t("Keep latest revisions based on date"),
        '#return_value' => 1,
        '#default_value' => isset($val) && $val ? 1 : 0,
      ];
      $form['fs_' . $machine_name]['interval__' . $machine_name] = [
        '#type' => 'select',
        '#title' => $this
          ->t("Keep latests revisions"),
        '#states' => [
          'visible' => [
            ':input[name="node_enable_date_' . $machine_name . '"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
        '#options' => [
          '0' => $this
            ->t('Choose value'),
          'P1W' => $this
            ->t('1 week'),
          'P2W' => $this
            ->t('2 weeks'),
          'P3W' => $this
            ->t('3 weeks'),
          'P1M' => $this
            ->t('1 month'),
          'P2M' => $this
            ->t('2 months'),
          'P3M' => $this
            ->t('3 months'),
          'P4M' => $this
            ->t('4 months'),
          'P5M' => $this
            ->t('5 months'),
          'P6M' => $this
            ->t('6 months'),
          'P1Y' => $this
            ->t('1 year'),
        ],
        '#default_value' => isset($val) && $val ? $val : 0,
      ];
    }
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Submit'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this->configFactory
      ->getEditable('node_revisions_autoclean.settings');
    $values = $form_state
      ->getValues();
    $enable_on_cron = (int) $form_state
      ->getValue('enable_on_cron');
    $config
      ->set('enable_on_cron', $enable_on_cron);
    $enable_on_node_update = (int) $form_state
      ->getValue('enable_on_node_update');
    $config
      ->set('enable_on_node_update', $enable_on_node_update);
    foreach ($values as $key => $val) {
      if (strpos($key, 'interval__') === 0) {
        $machine_name = str_replace('interval__', '', $key);
        $key = str_replace('__', '.', $key);
        if ($form_state
          ->getValue('node_enable_date_' . $machine_name)) {
          $config
            ->set($key, "{$val}");
        }
        else {
          $config
            ->set($key, '0');
        }
      }
      elseif (strpos($key, 'node__') === 0) {
        $key = str_replace('__', '.', $key);
        $config
          ->set($key, (int) $val);
      }
    }
    $config
      ->save(TRUE);
    $this
      ->messenger()
      ->addMessage($this
      ->t('Node revisions settings have been updated.'));
  }

}

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::$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.
NodeRevisionsByBundleForm::$configFactory protected property ConfigFactory. Overrides FormBase::$configFactory
NodeRevisionsByBundleForm::$entityTypeBundleInfo protected property EntityTypeBundleInfo.
NodeRevisionsByBundleForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
NodeRevisionsByBundleForm::create public static function Creates. Overrides FormBase::create
NodeRevisionsByBundleForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
NodeRevisionsByBundleForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
NodeRevisionsByBundleForm::__construct public function NodeRevisionsByBundleForm 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.