class NodeRevisionGenerateForm in Node Revision Delete 8
Class NodeRevisionGenerate.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\node_revision_generate\Form\NodeRevisionGenerateForm
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\FormView 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
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 config factory. | 1 |
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. | |
NodeRevisionGenerateForm:: |
protected | property | Drupal\Core\Entity\EntityTypeManagerInterface definition. | |
NodeRevisionGenerateForm:: |
protected | property | The node revision generate interface. | |
NodeRevisionGenerateForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
NodeRevisionGenerateForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
NodeRevisionGenerateForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
NodeRevisionGenerateForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
NodeRevisionGenerateForm:: |
public | function | 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. |