class NodeRevisionsByBundleForm in Node Revisions Autoclean 8
Class NodeRevisionsByBundleForm.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait- class \Drupal\node_revisions_autoclean\Form\NodeRevisionsByBundleForm
 
Expanded class hierarchy of NodeRevisionsByBundleForm
1 string reference to 'NodeRevisionsByBundleForm'
File
- src/Form/ NodeRevisionsByBundleForm.php, line 14 
Namespace
Drupal\node_revisions_autoclean\FormView 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
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| DependencySerializationTrait:: | protected | property | An array of entity type IDs keyed by the property name of their storages. | |
| DependencySerializationTrait:: | protected | property | An array of service IDs keyed by property name used for serialization. | |
| DependencySerializationTrait:: | public | function | 1 | |
| DependencySerializationTrait:: | public | function | 2 | |
| FormBase:: | protected | property | The request stack. | 1 | 
| FormBase:: | protected | property | The route match. | |
| FormBase:: | protected | function | Retrieves a configuration object. | |
| FormBase:: | protected | function | Gets the config factory for this form. | 1 | 
| FormBase:: | private | function | Returns the service container. | |
| FormBase:: | protected | function | Gets the current user. | |
| FormBase:: | protected | function | Gets the request object. | |
| FormBase:: | protected | function | Gets the route match. | |
| FormBase:: | protected | function | Gets the logger for a specific channel. | |
| FormBase:: | protected | function | Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: | |
| FormBase:: | public | function | Resets the configuration factory. | |
| FormBase:: | public | function | Sets the config factory for this form. | |
| FormBase:: | public | function | Sets the request stack object to use. | |
| FormBase:: | public | function | Form validation handler. Overrides FormInterface:: | 62 | 
| LinkGeneratorTrait:: | protected | property | The link generator. | 1 | 
| LinkGeneratorTrait:: | protected | function | Returns the link generator. | |
| LinkGeneratorTrait:: | protected | function | Renders a link to a route given a route name and its parameters. | |
| LinkGeneratorTrait:: | public | function | Sets the link generator service. | |
| LoggerChannelTrait:: | protected | property | The logger channel factory service. | |
| LoggerChannelTrait:: | protected | function | Gets the logger for a specific channel. | |
| LoggerChannelTrait:: | public | function | Injects the logger channel factory. | |
| MessengerTrait:: | protected | property | The messenger. | 29 | 
| MessengerTrait:: | public | function | Gets the messenger. | 29 | 
| MessengerTrait:: | public | function | Sets the messenger. | |
| NodeRevisionsByBundleForm:: | protected | property | ConfigFactory. Overrides FormBase:: | |
| NodeRevisionsByBundleForm:: | protected | property | EntityTypeBundleInfo. | |
| NodeRevisionsByBundleForm:: | public | function | Form constructor. Overrides FormInterface:: | |
| NodeRevisionsByBundleForm:: | public static | function | Creates. Overrides FormBase:: | |
| NodeRevisionsByBundleForm:: | public | function | Returns a unique string identifying the form. Overrides FormInterface:: | |
| NodeRevisionsByBundleForm:: | public | function | Form submission handler. Overrides FormInterface:: | |
| NodeRevisionsByBundleForm:: | public | function | NodeRevisionsByBundleForm constructor. | |
| RedirectDestinationTrait:: | protected | property | The redirect destination service. | 1 | 
| RedirectDestinationTrait:: | protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
| RedirectDestinationTrait:: | protected | function | Returns the redirect destination service. | |
| RedirectDestinationTrait:: | public | function | Sets the redirect destination service. | |
| StringTranslationTrait:: | protected | property | The string translation service. | 1 | 
| StringTranslationTrait:: | protected | function | Formats a string containing a count of items. | |
| StringTranslationTrait:: | protected | function | Returns the number of plurals supported by a given language. | |
| StringTranslationTrait:: | protected | function | Gets the string translation service. | |
| StringTranslationTrait:: | public | function | Sets the string translation service to use. | 2 | 
| StringTranslationTrait:: | protected | function | Translates a string to the current language or to a given language. | |
| UrlGeneratorTrait:: | protected | property | The url generator. | |
| UrlGeneratorTrait:: | protected | function | Returns the URL generator service. | |
| UrlGeneratorTrait:: | public | function | Sets the URL generator service. | |
| UrlGeneratorTrait:: | protected | function | Generates a URL or path for a specific route based on the given parameters. | 
