diff --git a/pathauto.services.yml b/pathauto.services.yml index 3ce704e..97bb4c3 100644 --- a/pathauto.services.yml +++ b/pathauto.services.yml @@ -1,7 +1,7 @@ services: pathauto.generator: class: Drupal\pathauto\PathautoGenerator - arguments: ['@config.factory', '@module_handler', '@token', '@pathauto.alias_cleaner', '@pathauto.alias_storage_helper', '@pathauto.alias_uniquifier', '@pathauto.verbose_messenger', '@string_translation', '@token.entity_mapper', '@entity_type.manager'] + arguments: ['@messenger', '@config.factory', '@module_handler', '@token', '@pathauto.alias_cleaner', '@pathauto.alias_storage_helper', '@pathauto.alias_uniquifier', '@pathauto.verbose_messenger', '@string_translation', '@token.entity_mapper', '@entity_type.manager'] pathauto.alias_cleaner: class: Drupal\pathauto\AliasCleaner arguments: ['@config.factory', '@pathauto.alias_storage_helper', '@language_manager', '@cache.discovery', '@transliteration', '@module_handler'] diff --git a/src/Form/PathautoAdminDelete.php b/src/Form/PathautoAdminDelete.php index a230525..075d305 100644 --- a/src/Form/PathautoAdminDelete.php +++ b/src/Form/PathautoAdminDelete.php @@ -4,6 +4,7 @@ namespace Drupal\pathauto\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Messenger\MessengerInterface; use Drupal\pathauto\AliasTypeManager; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -20,13 +21,24 @@ class PathautoAdminDelete extends FormBase { protected $aliasTypeManager; /** + * Drupal messenger service. + * + * @var \Drupal\Core\Messenger\MessengerInterface + */ + protected $messengerService; + + /** * Constructs a PathautoAdminDelete object. * * @param \Drupal\pathauto\AliasTypeManager $alias_type_manager - * The alias type manager. + * + * Constructs a MessengerInterface object. + * + * @param \Drupal\Core\Messenger\MessengerInterface $messenger */ - public function __construct(AliasTypeManager $alias_type_manager) { + public function __construct(AliasTypeManager $alias_type_manager, MessengerInterface $messenger) { $this->aliasTypeManager = $alias_type_manager; + $this->messengerService = $messenger; } /** @@ -34,7 +46,8 @@ class PathautoAdminDelete extends FormBase { */ public static function create(ContainerInterface $container) { return new static( - $container->get('plugin.manager.alias_type') + $container->get('plugin.manager.alias_type'), + $container->get('messenger') ); } @@ -133,14 +146,16 @@ class PathautoAdminDelete extends FormBase { } else if ($delete_all) { \Drupal::service('pathauto.alias_storage_helper')->deleteAll(); - drupal_set_message($this->t('All of your path aliases have been deleted.')); + $this->messengerService->addMessage($this->t('All of your path aliases have been deleted.')); } else { $storage_helper = \Drupal::service('pathauto.alias_storage_helper'); foreach (array_keys(array_filter($form_state->getValue(['delete', 'plugins']))) as $id) { $alias_type = $this->aliasTypeManager->createInstance($id); $storage_helper->deleteBySourcePrefix((string) $alias_type->getSourcePrefix()); - drupal_set_message($this->t('All of your %label path aliases have been deleted.', ['%label' => $alias_type->getLabel()])); + $this->messengerService->addMessage($this->t('All of your %label path aliases have been deleted.', [ + '%label' => $alias_type->getLabel(), + ])); } } } @@ -168,17 +183,22 @@ class PathautoAdminDelete extends FormBase { public static function batchFinished($success, $results, $operations) { if ($success) { if ($results['delete_all']) { - drupal_set_message(t('All of your automatically generated path aliases have been deleted.')); + \Drupal::service('messenger')->addMessage(t('All of your automatically generated path aliases have been deleted.')); } else if (isset($results['deletions'])) { foreach (array_values($results['deletions']) as $label) { - drupal_set_message(t('All of your automatically generated %label path aliases have been deleted.', ['%label' => $label])); + \Drupal::service('messenger')->addMessage(t('All of your automatically generated %label path aliases have been deleted.',[ + '%label' => $label, + ])); } } } else { $error_operation = reset($operations); - drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array('@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE)))); + \Drupal::service('messenger')->addMessage(t('An error occurred while processing @operation with arguments : @args',[ + '@operation' => $error_operation[0], + '@args' => print_r($error_operation[0]), + ])); } } diff --git a/src/Form/PathautoBulkUpdateForm.php b/src/Form/PathautoBulkUpdateForm.php index e66cd9d..72fa2fa 100644 --- a/src/Form/PathautoBulkUpdateForm.php +++ b/src/Form/PathautoBulkUpdateForm.php @@ -149,15 +149,18 @@ class PathautoBulkUpdateForm extends FormBase { public static function batchFinished($success, $results, $operations) { if ($success) { if ($results['updates']) { - drupal_set_message(\Drupal::translation()->formatPlural($results['updates'], 'Generated 1 URL alias.', 'Generated @count URL aliases.')); + \Drupal::service('messenger')->addMessage(\Drupal::translation()->formatPlural($results['updates'], 'Generated 1 URL alias.', 'Generated @count URL aliases.')); } else { - drupal_set_message(t('No new URL aliases to generate.')); + \Drupal::service('messenger')->addMessage(t('No new URL aliases to generate.')); } } else { $error_operation = reset($operations); - drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array('@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE)))); + \Drupal::service('messenger')->addMessage(t('An error occurred while processing @operation with arguments : @args'),[ + '@operation' => $error_operation[0], + '@args' => print_r($error_operation[0]) + ]); } } diff --git a/src/Form/PatternDisableForm.php b/src/Form/PatternDisableForm.php index c87b88b..9b6b528 100644 --- a/src/Form/PatternDisableForm.php +++ b/src/Form/PatternDisableForm.php @@ -4,6 +4,8 @@ namespace Drupal\pathauto\Form; use Drupal\Core\Entity\EntityConfirmFormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Messenger\MessengerInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Url; /** @@ -12,6 +14,31 @@ use Drupal\Core\Url; class PatternDisableForm extends EntityConfirmFormBase { /** + * Drupal messenger service. + * + * @var \Drupal\Core\Messenger\MessengerInterface + */ + protected $messengerService; + + /** + * PatternDisableForm constructor. + * + * @param \Drupal\Core\Messenger\MessengerInterface $messenger + */ + public function __construct(MessengerInterface $messenger) { + $this->messengerService = $messenger; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('messenger') + ); + } + + /** * {@inheritdoc} */ public function getQuestion() { @@ -44,7 +71,9 @@ class PatternDisableForm extends EntityConfirmFormBase { */ public function submitForm(array &$form, FormStateInterface $form_state) { $this->entity->disable()->save(); - drupal_set_message($this->t('Disabled pattern %label.', array('%label' => $this->entity->label()))); + $this->messengerService->addMessage($this->t('Disabled pattern %label.',[ + '%label' => $this->entity->label() + ])); $form_state->setRedirectUrl($this->getCancelUrl()); } diff --git a/src/Form/PatternEditForm.php b/src/Form/PatternEditForm.php index 49efc7e..f142c92 100644 --- a/src/Form/PatternEditForm.php +++ b/src/Form/PatternEditForm.php @@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageManagerInterface; +use Drupal\Core\Messenger\MessengerInterface; use Drupal\pathauto\AliasTypeManager; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -43,6 +44,11 @@ class PatternEditForm extends EntityForm { protected $languageManager; /** + * @var \Drupal\Core\Messenger\MessengerInterface + */ + protected $messengerService; + + /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { @@ -50,7 +56,8 @@ class PatternEditForm extends EntityForm { $container->get('plugin.manager.alias_type'), $container->get('entity_type.bundle.info'), $container->get('entity_type.manager'), - $container->get('language_manager') + $container->get('language_manager'), + $container->get('messenger') ); } @@ -61,12 +68,14 @@ class PatternEditForm extends EntityForm { * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager + * @param \Drupal\Core\Messenger\MessengerInterface $messenger */ - function __construct(AliasTypeManager $manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager) { + function __construct(AliasTypeManager $manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager, MessengerInterface $messenger) { $this->manager = $manager; $this->entityTypeBundleInfo = $entity_type_bundle_info; $this->entityTypeManager = $entity_type_manager; $this->languageManager = $language_manager; + $this->messengerService = $messenger; } /** @@ -246,7 +255,7 @@ class PatternEditForm extends EntityForm { ] ] ); - $entity->addRelationship($language_mapping, t('Language')); + $entity->addRelationship($language_mapping, $this->t('Language')); } } @@ -263,7 +272,9 @@ class PatternEditForm extends EntityForm { */ public function save(array $form, FormStateInterface $form_state) { parent::save($form, $form_state); - drupal_set_message($this->t('Pattern @label saved.', ['@label' => $this->entity->label()])); + $this->messengerService->addMessage($this->t('Pattern %label saved.', [ + '%label' => $this->entity->label(), + ])); $form_state->setRedirectUrl($this->entity->toUrl('collection')); } diff --git a/src/Form/PatternEnableForm.php b/src/Form/PatternEnableForm.php index 253ca31..08e201a 100644 --- a/src/Form/PatternEnableForm.php +++ b/src/Form/PatternEnableForm.php @@ -4,6 +4,8 @@ namespace Drupal\pathauto\Form; use Drupal\Core\Entity\EntityConfirmFormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Messenger\MessengerInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Url; /** @@ -12,6 +14,28 @@ use Drupal\Core\Url; class PatternEnableForm extends EntityConfirmFormBase { /** + * @var \Drupal\Core\Messenger\MessengerInterface + */ + protected $messengerService; + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('messenger') + ); + } + + /** + * PatternEnableForm constructor. + * + * @param \Drupal\Core\Messenger\MessengerInterface $messenger + */ + function __construct(MessengerInterface $messenger) { + $this->messengerService = $messenger; + } + /** * {@inheritdoc} */ public function getQuestion() { @@ -44,7 +68,9 @@ class PatternEnableForm extends EntityConfirmFormBase { */ public function submitForm(array &$form, FormStateInterface $form_state) { $this->entity->enable()->save(); - drupal_set_message($this->t('Enabled pattern %label.', array('%label' => $this->entity->label()))); + $this->messengerService->addMessage($this->t('Enabled pattern %label.', [ + '%label' => $this->entity->label(), + ])); $form_state->setRedirectUrl($this->getCancelUrl()); } diff --git a/src/PathautoGenerator.php b/src/PathautoGenerator.php index 6c56a5c..a568f80 100644 --- a/src/PathautoGenerator.php +++ b/src/PathautoGenerator.php @@ -15,13 +15,20 @@ use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\Utility\Token; use Drupal\token\TokenEntityMapperInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Messenger\MessengerInterface; /** * Provides methods for generating path aliases. */ class PathautoGenerator implements PathautoGeneratorInterface { - use StringTranslationTrait; +use StringTranslationTrait; + /** + * The messenger service. + * + * @var \Drupal\Core\Messenger\MessengerInterface + */ + protected $messenger; /** * Config factory. @@ -80,13 +87,6 @@ class PathautoGenerator implements PathautoGeneratorInterface { protected $aliasUniquifier; /** - * The messenger service. - * - * @var \Drupal\pathauto\MessengerInterface - */ - protected $messenger; - - /** * @var \Drupal\token\TokenEntityMapperInterface */ protected $tokenEntityMapper; @@ -118,7 +118,7 @@ class PathautoGenerator implements PathautoGeneratorInterface { * @param Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager */ - public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, Token $token, AliasCleanerInterface $alias_cleaner, AliasStorageHelperInterface $alias_storage_helper, AliasUniquifierInterface $alias_uniquifier, MessengerInterface $messenger, TranslationInterface $string_translation, TokenEntityMapperInterface $token_entity_mappper, EntityTypeManagerInterface $entity_type_manager) { + public function __construct(MessengerInterface $messenger, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, Token $token, AliasCleanerInterface $alias_cleaner, AliasStorageHelperInterface $alias_storage_helper, AliasUniquifierInterface $alias_uniquifier, TranslationInterface $string_translation, TokenEntityMapperInterface $token_entity_mappper, EntityTypeManagerInterface $entity_type_manager) { $this->configFactory = $config_factory; $this->moduleHandler = $module_handler; $this->token = $token; @@ -335,7 +335,7 @@ class PathautoGenerator implements PathautoGeneratorInterface { $result = $this->createEntityAlias($entity, $op); } catch (\InvalidArgumentException $e) { - drupal_set_message($e->getMessage(), 'error'); + $this->messenger->addWarning($e->getMessage()); return NULL; } diff --git a/src/Plugin/pathauto/AliasType/EntityAliasTypeBase.php b/src/Plugin/pathauto/AliasType/EntityAliasTypeBase.php index c612a2b..e6b7ada 100644 --- a/src/Plugin/pathauto/AliasType/EntityAliasTypeBase.php +++ b/src/Plugin/pathauto/AliasType/EntityAliasTypeBase.php @@ -15,6 +15,7 @@ use Drupal\pathauto\AliasTypeBatchUpdateInterface; use Drupal\pathauto\AliasTypeInterface; use Drupal\pathauto\PathautoState; use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Messenger\MessengerInterface; /** * A pathauto alias type plugin for entities with canonical links. @@ -25,6 +26,12 @@ use Symfony\Component\DependencyInjection\ContainerInterface; * ) */ class EntityAliasTypeBase extends ContextAwarePluginBase implements AliasTypeInterface, AliasTypeBatchUpdateInterface, ContainerFactoryPluginInterface { + /** + * The messenger service. + * + * @var \Drupal\Core\Messenger\MessengerInterface + */ + protected $messenger; /** * The module handler service. @@ -87,14 +94,17 @@ class EntityAliasTypeBase extends ContextAwarePluginBase implements AliasTypeInt * The key/value manager service. * @param \Drupal\Core\Database\Connection $database * The database connection. + * @param MessengerInterface $messenger + * The messenger service. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_type_manager, KeyValueFactoryInterface $key_value, Connection $database) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, ModuleHandlerInterface $module_handler, LanguageManagerInterface $language_manager, EntityTypeManagerInterface $entity_type_manager, KeyValueFactoryInterface $key_value, Connection $database, MessengerInterface $messenger) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->moduleHandler = $module_handler; $this->languageManager = $language_manager; $this->entityTypeManager = $entity_type_manager; $this->keyValue = $key_value; $this->database = $database; + $this->messenger = $messenger; } /** @@ -109,7 +119,9 @@ class EntityAliasTypeBase extends ContextAwarePluginBase implements AliasTypeInt $container->get('language_manager'), $container->get('entity_type.manager'), $container->get('keyvalue'), - $container->get('database') + $container->get('database'), + $container->get('messenger') + ); } @@ -274,7 +286,9 @@ class EntityAliasTypeBase extends ContextAwarePluginBase implements AliasTypeInt } if (!empty($options['message'])) { - drupal_set_message(\Drupal::translation()->formatPlural(count($ids), 'Updated 1 %label URL alias.', 'Updated @count %label URL aliases.'), array('%label' => $this->getLabel())); + $this->messenger->addMessage(\Drupal::translation()->formatPlural(count($ids), 'Updated 1 %label URL alias.', 'Updated @count %label URL aliases.'), [ + '%label' => $this->getLabel(), + ]); } return $updates;