3 namespace Drupal\pathauto\Plugin\pathauto\AliasType;
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Drupal\Core\Entity\FieldableEntityInterface;
8 use Drupal\Core\Extension\ModuleHandlerInterface;
9 use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
10 use Drupal\Core\Language\LanguageManagerInterface;
11 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
12 use Drupal\Core\Plugin\Context\Context;
13 use Drupal\Core\Plugin\ContextAwarePluginBase;
14 use Drupal\Core\Messenger\MessengerTrait;
15 use Drupal\pathauto\AliasTypeBatchUpdateInterface;
16 use Drupal\pathauto\AliasTypeInterface;
17 use Drupal\pathauto\PathautoState;
18 use Symfony\Component\DependencyInjection\ContainerInterface;
21 * A pathauto alias type plugin for entities with canonical links.
24 * id = "canonical_entities",
25 * deriver = "\Drupal\pathauto\Plugin\Deriver\EntityAliasTypeDeriver"
28 class EntityAliasTypeBase extends ContextAwarePluginBase implements AliasTypeInterface, AliasTypeBatchUpdateInterface, ContainerFactoryPluginInterface {
33 * The module handler service.
35 * @var \Drupal\Core\Extension\ModuleHandlerInterface
37 protected $moduleHandler;
40 * The language manager service.
42 * @var \Drupal\Core\Language\LanguageManagerInterface
44 protected $languageManager;
47 * The entity manager service.
49 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
51 protected $entityTypeManager;
54 * The key/value manager service.
56 * @var \Drupal\Core\KeyValueStore\KeyValueFactoryInterface
61 * The database connection.
63 * @var \Drupal\Core\Database\Connection
68 * The path prefix for this entity type.
75 * Constructs a EntityAliasTypeBase instance.
77 * @param array $configuration
78 * A configuration array containing information about the plugin instance.
79 * @param string $plugin_id
80 * The plugin_id for the plugin instance.
81 * @param mixed $plugin_definition
82 * The plugin implementation definition.
83 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
84 * The module handler service.
85 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
86 * The language manager service.
87 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
88 * The entity manager service.
89 * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value
90 * The key/value manager service.
91 * @param \Drupal\Core\Database\Connection $database
92 * The database connection.
94 public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_type_manager, KeyValueFactoryInterface $key_value, Connection $database) {
95 parent::__construct($configuration, $plugin_id, $plugin_definition);
96 $this->moduleHandler = $module_handler;
97 $this->languageManager = $language_manager;
98 $this->entityTypeManager = $entity_type_manager;
99 $this->keyValue = $key_value;
100 $this->database = $database;
106 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
111 $container->get('module_handler'),
112 $container->get('language_manager'),
113 $container->get('entity_type.manager'),
114 $container->get('keyvalue'),
115 $container->get('database')
122 public function getLabel() {
123 $definition = $this->getPluginDefinition();
124 // Cast the admin label to a string since it is an object.
125 // @see \Drupal\Core\StringTranslation\TranslationWrapper
126 return (string) $definition['label'];
132 public function getTokenTypes() {
133 $definition = $this->getPluginDefinition();
134 return $definition['types'];
140 public function batchUpdate($action, &$context) {
141 if (!isset($context['sandbox']['current'])) {
142 $context['sandbox']['count'] = 0;
143 $context['sandbox']['current'] = 0;
146 $entity_type = $this->entityTypeManager->getDefinition($this->getEntityTypeId());
147 $id_key = $entity_type->getKey('id');
149 $query = $this->database->select($entity_type->get('base_table'), 'base_table');
150 $query->leftJoin('url_alias', 'ua', "CONCAT('" . $this->getSourcePrefix() . "' , base_table.$id_key) = ua.source");
151 $query->addField('base_table', $id_key, 'id');
155 $query->isNull('ua.source');
159 $query->isNotNull('ua.source');
163 // Nothing to do. We want all paths.
167 // Unknown action. Abort!
170 $query->condition('base_table.' . $id_key, $context['sandbox']['current'], '>');
171 $query->orderBy('base_table.' . $id_key);
172 $query->addTag('pathauto_bulk_update');
173 $query->addMetaData('entity', $this->getEntityTypeId());
175 // Get the total amount of items to process.
176 if (!isset($context['sandbox']['total'])) {
177 $context['sandbox']['total'] = $query->countQuery()->execute()->fetchField();
179 // If there are no entities to update, then stop immediately.
180 if (!$context['sandbox']['total']) {
181 $context['finished'] = 1;
186 $query->range(0, 25);
187 $ids = $query->execute()->fetchCol();
189 $updates = $this->bulkUpdate($ids);
190 $context['sandbox']['count'] += count($ids);
191 $context['sandbox']['current'] = !empty($ids) ? max($ids) : 0;
192 $context['results']['updates'] += $updates;
193 $context['message'] = $this->t('Updated alias for %label @id.', array('%label' => $entity_type->getLabel(), '@id' => end($ids)));
195 if ($context['sandbox']['count'] != $context['sandbox']['total']) {
196 $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
203 public function batchDelete(&$context) {
204 if (!isset($context['sandbox']['current'])) {
205 $context['sandbox']['count'] = 0;
206 $context['sandbox']['current'] = 0;
209 $entity_type = $this->entityTypeManager->getDefinition($this->getEntityTypeId());
210 $id_key = $entity_type->getKey('id');
212 $query = $this->database->select($entity_type->get('base_table'), 'base_table');
213 $query->innerJoin('url_alias', 'ua', "CONCAT('" . $this->getSourcePrefix() . "' , base_table.$id_key) = ua.source");
214 $query->addField('base_table', $id_key, 'id');
215 $query->addField('ua', 'pid');
216 $query->condition('ua.pid', $context['sandbox']['current'], '>');
217 $query->orderBy('ua.pid');
218 $query->addTag('pathauto_bulk_delete');
219 $query->addMetaData('entity', $this->getEntityTypeId());
221 // Get the total amount of items to process.
222 if (!isset($context['sandbox']['total'])) {
223 $context['sandbox']['total'] = $query->countQuery()->execute()->fetchField();
225 // If there are no entities to delete, then stop immediately.
226 if (!$context['sandbox']['total']) {
227 $context['finished'] = 1;
232 $query->range(0, 100);
233 $pids_by_id = $query->execute()->fetchAllKeyed();
235 PathautoState::bulkDelete($this->getEntityTypeId(), $pids_by_id);
236 $context['sandbox']['count'] += count($pids_by_id);
237 $context['sandbox']['current'] = max($pids_by_id);
238 $context['results']['deletions'][] = $this->getLabel();
240 if ($context['sandbox']['count'] != $context['sandbox']['total']) {
241 $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
246 * Returns the entity type ID.
249 * The entity type ID.
251 protected function getEntityTypeId() {
252 return $this->getDerivativeId();
256 * Update the URL aliases for multiple entities.
259 * An array of entity IDs.
260 * @param array $options
261 * An optional array of additional options.
264 * The number of updated URL aliases.
266 protected function bulkUpdate(array $ids, array $options = array()) {
267 $options += array('message' => FALSE);
270 $entities = $this->entityTypeManager->getStorage($this->getEntityTypeId())->loadMultiple($ids);
271 foreach ($entities as $entity) {
272 // Update aliases for the entity's default language and its translations.
273 foreach ($entity->getTranslationLanguages() as $langcode => $language) {
274 $translated_entity = $entity->getTranslation($langcode);
275 $result = \Drupal::service('pathauto.generator')->updateEntityAlias($translated_entity, 'bulkupdate', $options);
282 if (!empty($options['message'])) {
283 $this->messenger->addMessage($this->translationManager
284 ->formatPlural(count($ids), 'Updated 1 %label URL alias.', 'Updated @count %label URL aliases.'), [
285 '%label' => $this->getLabel(),
293 * Deletes the URL aliases for multiple entities.
295 * @param int[] $pids_by_id
296 * A list of path IDs keyed by entity ID.
298 * @deprecated Use \Drupal\pathauto\PathautoState::bulkDelete() instead.
300 protected function bulkDelete(array $pids_by_id) {
301 PathautoState::bulkDelete($this->getEntityTypeId(), $pids_by_id);
307 public function calculateDependencies() {
309 $dependencies['module'][] = $this->entityTypeManager->getDefinition($this->getEntityTypeId())->getProvider();
310 return $dependencies;
316 public function applies($object) {
317 return $object instanceof FieldableEntityInterface && $object->getEntityTypeId() == $this->getEntityTypeId();
323 public function getSourcePrefix() {
324 if (empty($this->prefix)) {
325 $entity_type = $this->entityTypeManager->getDefinition($this->getEntityTypeId());
326 $path = $entity_type->getLinkTemplate('canonical');
327 $this->prefix = substr($path, 0, strpos($path, '{'));
329 return $this->prefix;
335 public function setContextValue($name, $value) {
336 // Overridden to avoid merging existing cacheability metadata, which is not
337 // relevant for alias type plugins.
338 $this->context[$name] = new Context($this->getContextDefinition($name), $value);