Guest User

Untitled

a guest
Jan 24th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\node\Plugin\Action;
  4.  
  5. use Drupal\Core\Action\ConfigurableActionBase;
  6. use Drupal\Core\Database\Connection;
  7. use Drupal\Core\Form\FormStateInterface;
  8. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  9. use Drupal\Core\Session\AccountInterface;
  10. use Drupal\Core\Datetime\Plugin\Field\FieldWidget;
  11. use Drupal\Core\Datetime\DrupalDateTime;
  12. use Drupal\Core\Datetime\Element\Datetime;
  13. use Drupal\Core\Datetime\Entity\DateFormat;
  14. use Drupal\Core\Field\FieldItemListInterface;
  15. use Drupal\Core\Field\WidgetBase;
  16. use Symfony\Component\DependencyInjection\ContainerInterface;
  17.  
  18. /**
  19. * Change created date of a node.
  20. *
  21. * @Action(
  22. * id = "node_created_action",
  23. * label = @Translation("Change creation date for selected content"),
  24. * type = "node"
  25. * )
  26. */
  27. class CreatedNode extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
  28.  
  29. /**
  30. * The database connection.
  31. *
  32. * @var \Drupal\Core\Database\Connection
  33. */
  34. protected $connection;
  35.  
  36. /**
  37. * Constructs a new AssignOwnerNode action.
  38. *
  39. * @param array $configuration
  40. * A configuration array containing information about the plugin instance.
  41. * @param string $plugin_id
  42. * The plugin ID for the plugin instance.
  43. * @param mixed $plugin_definition
  44. * The plugin implementation definition.
  45. * @param \Drupal\Core\Database\Connection $connection
  46. * The database connection.
  47. */
  48. public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $connection) {
  49. parent::__construct($configuration, $plugin_id, $plugin_definition);
  50.  
  51. $this->connection = $connection;
  52. }
  53.  
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  58. return new static($configuration, $plugin_id, $plugin_definition,
  59. $container->get('database')
  60. );
  61. }
  62.  
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function execute($entity = NULL) {
  67. /** @var \Drupal\node\NodeInterface $entity */
  68. $entity->setCreatedTime($this->configuration['created'])->save();
  69. }
  70.  
  71.  
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  76. $description = t('Insert timestamp');
  77. $element = [];
  78. $date_format = DateFormat::load('html_date')->getPattern();
  79. $time_format = DateFormat::load('html_time')->getPattern();
  80. $default_value = DrupalDateTime::createFromTimestamp(time());
  81. $element['value'] = $element + [
  82. '#type' => 'datetime',
  83. '#default_value' => $default_value,
  84. '#date_year_range' => '1902:2037',
  85. ];
  86. $element['value']['#description'] = $this->t('Format: %format. Leave blank to use the time of form submission.', ['%format' => Datetime::formatExample($date_format . ' ' . $time_format)]);
  87. $form['createdDate'] = $element['value'];
  88. return $form;
  89. }
  90.  
  91.  
  92.  
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
  97. $dateTime = $form_state->getValue('createdDate')->getTimestamp();
  98. $this->configuration['created'] = $dateTime;
  99. }
  100.  
  101. /**
  102. * {@inheritdoc}
  103. */
  104. protected function getFieldsToUpdate() {
  105. return ['created' => time()];
  106. }
  107.  
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
  112. /** @var \Drupal\node\NodeInterface $object */
  113. $result = $object->access('update', $account, TRUE)
  114. ->andIf($object->getOwner()->access('edit', $account, TRUE));
  115.  
  116. return $return_as_object ? $result : $result->isAllowed();
  117. }
  118.  
  119. }
Add Comment
Please, Sign In to add comment