Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / node / src / Plugin / Action / AssignOwnerNode.php
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\user\Entity\User;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Assigns ownership of a node to a user.
15  *
16  * @Action(
17  *   id = "node_assign_owner_action",
18  *   label = @Translation("Change the author of content"),
19  *   type = "node"
20  * )
21  */
22 class AssignOwnerNode extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
23
24   /**
25    * The database connection.
26    *
27    * @var \Drupal\Core\Database\Connection
28    */
29   protected $connection;
30
31   /**
32    * Constructs a new AssignOwnerNode action.
33    *
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.
42    */
43   public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $connection) {
44     parent::__construct($configuration, $plugin_id, $plugin_definition);
45
46     $this->connection = $connection;
47   }
48
49   /**
50    * {@inheritdoc}
51    */
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')
55     );
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public function execute($entity = NULL) {
62     /** @var \Drupal\node\NodeInterface $entity */
63     $entity->setOwnerId($this->configuration['owner_uid'])->save();
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function defaultConfiguration() {
70     return [
71       'owner_uid' => '',
72     ];
73   }
74
75   /**
76    * {@inheritdoc}
77    */
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();
81
82     // Use dropdown for fewer than 200 users; textbox for more than that.
83     if (intval($count) < 200) {
84       $options = [];
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;
88       }
89       $form['owner_uid'] = [
90         '#type' => 'select',
91         '#title' => t('Username'),
92         '#default_value' => $this->configuration['owner_uid'],
93         '#options' => $options,
94         '#description' => $description,
95       ];
96     }
97     else {
98       $form['owner_uid'] = [
99         '#type' => 'entity_autocomplete',
100         '#title' => t('Username'),
101         '#target_type' => 'user',
102         '#selection_setttings' => [
103           'include_anonymous' => FALSE,
104         ],
105         '#default_value' => User::load($this->configuration['owner_uid']),
106         // Validation is done in static::validateConfigurationForm().
107         '#validate_reference' => FALSE,
108         '#size' => '6',
109         '#maxlength' => '60',
110         '#description' => $description,
111       ];
112     }
113     return $form;
114   }
115
116   /**
117    * {@inheritdoc}
118    */
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();
121     if (!$exists) {
122       $form_state->setErrorByName('owner_uid', t('Enter a valid username.'));
123     }
124   }
125
126   /**
127    * {@inheritdoc}
128    */
129   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
130     $this->configuration['owner_uid'] = $form_state->getValue('owner_uid');
131   }
132
133   /**
134    * {@inheritdoc}
135    */
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));
140
141     return $return_as_object ? $result : $result->isAllowed();
142   }
143
144 }