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\pathauto\AliasTypeBatchUpdateInterface;
15 use Drupal\pathauto\AliasTypeInterface;
16 use Drupal\pathauto\PathautoState;
17 use Symfony\Component\DependencyInjection\ContainerInterface;
20 * A pathauto alias type plugin for entities with canonical links.
23 * id = "canonical_entities",
24 * deriver = "\Drupal\pathauto\Plugin\Deriver\EntityAliasTypeDeriver"
27 class EntityAliasTypeBase extends ContextAwarePluginBase implements AliasTypeInterface, AliasTypeBatchUpdateInterface, ContainerFactoryPluginInterface {
30 * The module handler service.
32 * @var \Drupal\Core\Extension\ModuleHandlerInterface
34 protected $moduleHandler;
37 * The language manager service.
39 * @var \Drupal\Core\Language\LanguageManagerInterface
41 protected $languageManager;
44 * The entity manager service.
46 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
48 protected $entityTypeManager;
51 * The key/value manager service.
53 * @var \Drupal\Core\KeyValueStore\KeyValueFactoryInterface
58 * The database connection.
60 * @var \Drupal\Core\Database\Connection
65 * The path prefix for this entity type.
72 * Constructs a EntityAliasTypeBase instance.
74 * @param array $configuration
75 * A configuration array containing information about the plugin instance.
76 * @param string $plugin_id
77 * The plugin_id for the plugin instance.
78 * @param mixed $plugin_definition
79 * The plugin implementation definition.
80 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
81 * The module handler service.
82 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
83 * The language manager service.
84 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
85 * The entity manager service.
86 * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value
87 * The key/value manager service.
88 * @param \Drupal\Core\Database\Connection $database
89 * The database connection.
91 public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_type_manager, KeyValueFactoryInterface $key_value, Connection $database) {
92 parent::__construct($configuration, $plugin_id, $plugin_definition);
93 $this->moduleHandler = $module_handler;
94 $this->languageManager = $language_manager;
95 $this->entityTypeManager = $entity_type_manager;
96 $this->keyValue = $key_value;
97 $this->database = $database;
103 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
108 $container->get('module_handler'),
109 $container->get('language_manager'),
110 $container->get('entity_type.manager'),
111 $container->get('keyvalue'),
112 $container->get('database')
119 public function getLabel() {
120 $definition = $this->getPluginDefinition();
121 // Cast the admin label to a string since it is an object.
122 // @see \Drupal\Core\StringTranslation\TranslationWrapper
123 return (string) $definition['label'];
129 public function getTokenTypes() {
130 $definition = $this->getPluginDefinition();
131 return $definition['types'];
137 public function batchUpdate($action, &$context) {
138 if (!isset($context['sandbox']['current'])) {
139 $context['sandbox']['count'] = 0;
140 $context['sandbox']['current'] = 0;
143 $entity_type = $this->entityTypeManager->getDefinition($this->getEntityTypeId());
144 $id_key = $entity_type->getKey('id');
146 $query = $this->database->select($entity_type->get('base_table'), 'base_table');
147 $query->leftJoin('url_alias', 'ua', "CONCAT('" . $this->getSourcePrefix() . "' , base_table.$id_key) = ua.source");
148 $query->addField('base_table', $id_key, 'id');
152 $query->isNull('ua.source');
155 $query->isNotNull('ua.source');
158 // Nothing to do. We want all paths.
161 // Unknown action. Abort!
164 $query->condition('base_table.' . $id_key, $context['sandbox']['current'], '>');
165 $query->orderBy('base_table.' . $id_key);
166 $query->addTag('pathauto_bulk_update');
167 $query->addMetaData('entity', $this->getEntityTypeId());
169 // Get the total amount of items to process.
170 if (!isset($context['sandbox']['total'])) {
171 $context['sandbox']['total'] = $query->countQuery()->execute()->fetchField();
173 // If there are no entities to update, then stop immediately.
174 if (!$context['sandbox']['total']) {
175 $context['finished'] = 1;
180 $query->range(0, 25);
181 $ids = $query->execute()->fetchCol();
183 $updates = $this->bulkUpdate($ids);
184 $context['sandbox']['count'] += count($ids);
185 $context['sandbox']['current'] = max($ids);
186 $context['results']['updates'] += $updates;
187 $context['message'] = $this->t('Updated alias for %label @id.', array('%label' => $entity_type->getLabel(), '@id' => end($ids)));
189 if ($context['sandbox']['count'] != $context['sandbox']['total']) {
190 $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
197 public function batchDelete(&$context) {
198 if (!isset($context['sandbox']['current'])) {
199 $context['sandbox']['count'] = 0;
200 $context['sandbox']['current'] = 0;
203 $entity_type = $this->entityTypeManager->getDefinition($this->getEntityTypeId());
204 $id_key = $entity_type->getKey('id');
206 $query = $this->database->select($entity_type->get('base_table'), 'base_table');
207 $query->innerJoin('url_alias', 'ua', "CONCAT('" . $this->getSourcePrefix() . "' , base_table.$id_key) = ua.source");
208 $query->addField('base_table', $id_key, 'id');
209 $query->addField('ua', 'pid');
210 $query->condition('ua.pid', $context['sandbox']['current'], '>');
211 $query->orderBy('ua.pid');
212 $query->addTag('pathauto_bulk_delete');
213 $query->addMetaData('entity', $this->getEntityTypeId());
215 // Get the total amount of items to process.
216 if (!isset($context['sandbox']['total'])) {
217 $context['sandbox']['total'] = $query->countQuery()->execute()->fetchField();
219 // If there are no entities to delete, then stop immediately.
220 if (!$context['sandbox']['total']) {
221 $context['finished'] = 1;
226 $query->range(0, 100);
227 $pids_by_id = $query->execute()->fetchAllKeyed();
229 PathautoState::bulkDelete($this->getEntityTypeId(), $pids_by_id);
230 $context['sandbox']['count'] += count($pids_by_id);
231 $context['sandbox']['current'] = max($pids_by_id);
232 $context['results']['deletions'][] = $this->getLabel();
234 if ($context['sandbox']['count'] != $context['sandbox']['total']) {
235 $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
240 * Returns the entity type ID.
243 * The entity type ID.
245 protected function getEntityTypeId() {
246 return $this->getDerivativeId();
250 * Update the URL aliases for multiple entities.
253 * An array of entity IDs.
254 * @param array $options
255 * An optional array of additional options.
258 * The number of updated URL aliases.
260 protected function bulkUpdate(array $ids, array $options = array()) {
261 $options += array('message' => FALSE);
264 $entities = $this->entityTypeManager->getStorage($this->getEntityTypeId())->loadMultiple($ids);
265 foreach ($entities as $entity) {
266 // Update aliases for the entity's default language and its translations.
267 foreach ($entity->getTranslationLanguages() as $langcode => $language) {
268 $translated_entity = $entity->getTranslation($langcode);
269 $result = \Drupal::service('pathauto.generator')->updateEntityAlias($translated_entity, 'bulkupdate', $options);
276 if (!empty($options['message'])) {
277 drupal_set_message(\Drupal::translation()->formatPlural(count($ids), 'Updated 1 %label URL alias.', 'Updated @count %label URL aliases.'), array('%label' => $this->getLabel()));
284 * Deletes the URL aliases for multiple entities.
286 * @param int[] $pids_by_id
287 * A list of path IDs keyed by entity ID.
289 * @deprecated Use \Drupal\pathauto\PathautoState::bulkDelete() instead.
291 protected function bulkDelete(array $pids_by_id) {
292 PathautoState::bulkDelete($this->getEntityTypeId(), $pids_by_id);
298 public function calculateDependencies() {
300 $dependencies['module'][] = $this->entityTypeManager->getDefinition($this->getEntityTypeId())->getProvider();
301 return $dependencies;
307 public function applies($object) {
308 return $object instanceof FieldableEntityInterface && $object->getEntityTypeId() == $this->getEntityTypeId();
314 public function getSourcePrefix() {
315 if (empty($this->prefix)) {
316 $entity_type = $this->entityTypeManager->getDefinition($this->getEntityTypeId());
317 $path = $entity_type->getLinkTemplate('canonical');
318 $this->prefix = substr($path, 0, strpos($path, '{'));
320 return $this->prefix;
326 public function setContextValue($name, $value) {
327 // Overridden to avoid merging existing cacheability metadata, which is not
328 // relevant for alias type plugins.
329 $this->context[$name] = new Context($this->getContextDefinition($name), $value);