3 namespace Drupal\node\Plugin\Action;
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\user\Entity\User;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Assigns ownership of a node to a user.
17 * id = "node_assign_owner_action",
18 * label = @Translation("Change the author of content"),
22 class AssignOwnerNode extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
25 * The database connection.
27 * @var \Drupal\Core\Database\Connection
29 protected $connection;
32 * Constructs a new AssignOwnerNode action.
34 * @param array $configuration
35 * A configuration array containing information about the plugin instance.
36 * @param string $plugin_id
37 * The plugin ID for the plugin instance.
38 * @param mixed $plugin_definition
39 * The plugin implementation definition.
40 * @param \Drupal\Core\Database\Connection $connection
41 * The database connection.
43 public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $connection) {
44 parent::__construct($configuration, $plugin_id, $plugin_definition);
46 $this->connection = $connection;
52 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
53 return new static($configuration, $plugin_id, $plugin_definition,
54 $container->get('database')
61 public function execute($entity = NULL) {
62 /** @var \Drupal\node\NodeInterface $entity */
63 $entity->setOwnerId($this->configuration['owner_uid'])->save();
69 public function defaultConfiguration() {
78 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
79 $description = t('The username of the user to which you would like to assign ownership.');
80 $count = $this->connection->query("SELECT COUNT(*) FROM {users}")->fetchField();
82 // Use dropdown for fewer than 200 users; textbox for more than that.
83 if (intval($count) < 200) {
85 $result = $this->connection->query("SELECT uid, name FROM {users_field_data} WHERE uid > 0 AND default_langcode = 1 ORDER BY name");
86 foreach ($result as $data) {
87 $options[$data->uid] = $data->name;
89 $form['owner_uid'] = [
91 '#title' => t('Username'),
92 '#default_value' => $this->configuration['owner_uid'],
93 '#options' => $options,
94 '#description' => $description,
98 $form['owner_uid'] = [
99 '#type' => 'entity_autocomplete',
100 '#title' => t('Username'),
101 '#target_type' => 'user',
102 '#selection_setttings' => [
103 'include_anonymous' => FALSE,
105 '#default_value' => User::load($this->configuration['owner_uid']),
106 // Validation is done in static::validateConfigurationForm().
107 '#validate_reference' => FALSE,
109 '#maxlength' => '60',
110 '#description' => $description,
119 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
120 $exists = (bool) $this->connection->queryRange('SELECT 1 FROM {users_field_data} WHERE uid = :uid AND default_langcode = 1', 0, 1, [':uid' => $form_state->getValue('owner_uid')])->fetchField();
122 $form_state->setErrorByName('owner_uid', t('Enter a valid username.'));
129 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
130 $this->configuration['owner_uid'] = $form_state->getValue('owner_uid');
136 public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
137 /** @var \Drupal\node\NodeInterface $object */
138 $result = $object->access('update', $account, TRUE)
139 ->andIf($object->getOwner()->access('edit', $account, TRUE));
141 return $return_as_object ? $result : $result->isAllowed();