Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Drupal\node\Plugin\Action;
- use Drupal\Core\Action\ConfigurableActionBase;
- use Drupal\Core\Database\Connection;
- use Drupal\Core\Form\FormStateInterface;
- use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
- use Drupal\Core\Session\AccountInterface;
- use Drupal\Core\Datetime\Plugin\Field\FieldWidget;
- use Drupal\Core\Datetime\DrupalDateTime;
- use Drupal\Core\Datetime\Element\Datetime;
- use Drupal\Core\Datetime\Entity\DateFormat;
- use Drupal\Core\Field\FieldItemListInterface;
- use Drupal\Core\Field\WidgetBase;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- /**
- * Change created date of a node.
- *
- * @Action(
- * id = "node_created_action",
- * label = @Translation("Change creation date for selected content"),
- * type = "node"
- * )
- */
- class CreatedNode extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
- /**
- * The database connection.
- *
- * @var \Drupal\Core\Database\Connection
- */
- protected $connection;
- /**
- * Constructs a new AssignOwnerNode action.
- *
- * @param array $configuration
- * A configuration array containing information about the plugin instance.
- * @param string $plugin_id
- * The plugin ID for the plugin instance.
- * @param mixed $plugin_definition
- * The plugin implementation definition.
- * @param \Drupal\Core\Database\Connection $connection
- * The database connection.
- */
- public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $connection) {
- parent::__construct($configuration, $plugin_id, $plugin_definition);
- $this->connection = $connection;
- }
- /**
- * {@inheritdoc}
- */
- public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
- return new static($configuration, $plugin_id, $plugin_definition,
- $container->get('database')
- );
- }
- /**
- * {@inheritdoc}
- */
- public function execute($entity = NULL) {
- /** @var \Drupal\node\NodeInterface $entity */
- $entity->setCreatedTime($this->configuration['created'])->save();
- }
- /**
- * {@inheritdoc}
- */
- public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
- $description = t('Insert timestamp');
- $element = [];
- $date_format = DateFormat::load('html_date')->getPattern();
- $time_format = DateFormat::load('html_time')->getPattern();
- $default_value = DrupalDateTime::createFromTimestamp(time());
- $element['value'] = $element + [
- '#type' => 'datetime',
- '#default_value' => $default_value,
- '#date_year_range' => '1902:2037',
- ];
- $element['value']['#description'] = $this->t('Format: %format. Leave blank to use the time of form submission.', ['%format' => Datetime::formatExample($date_format . ' ' . $time_format)]);
- $form['createdDate'] = $element['value'];
- return $form;
- }
- /**
- * {@inheritdoc}
- */
- public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
- $dateTime = $form_state->getValue('createdDate')->getTimestamp();
- $this->configuration['created'] = $dateTime;
- }
- /**
- * {@inheritdoc}
- */
- protected function getFieldsToUpdate() {
- return ['created' => time()];
- }
- /**
- * {@inheritdoc}
- */
- public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
- /** @var \Drupal\node\NodeInterface $object */
- $result = $object->access('update', $account, TRUE)
- ->andIf($object->getOwner()->access('edit', $account, TRUE));
- return $return_as_object ? $result : $result->isAllowed();
- }
- }
Add Comment
Please, Sign In to add comment