- Issue created by @osopolar
- π©πͺGermany osopolar π©πͺ GER π
For now we did not implemented it as suggested but use a workaround in our custom helper module as suggested under https://drupal.stackexchange.com/questions/312612/how-can-i-run-the-inbu...
namespace Drupal\custom_helper\Commands; use Consolidation\AnnotatedCommand\CommandData; use Drupal\Core\Session\AccountSwitcherInterface; use Drupal\Core\Session\UserSession; use Drush\Commands\DrushCommands; /** * A Drush commandfile. */ class VgwortRunAsAdmin extends DrushCommands { /** * The account switcher service. * * @var \Drupal\Core\Session\AccountSwitcherInterface */ protected $accountSwitcher; /** * @param \Drupal\Core\Session\AccountSwitcherInterface $account_switcher * The account switching service. */ public function __construct(AccountSwitcherInterface $account_switcher) { $this->accountSwitcher = $account_switcher; } /** * @hook pre-command vgwort:send */ public function preCommand(CommandData $commandData) { $this->accountSwitcher->switchTo(new UserSession(['uid' => 1])); } /** * @hook post-command vgwort:send */ public function postCommand($result, CommandData $commandData) { $this->accountSwitcher->switchBack(); } }
And add the @account_switcher service to the custom helper modules drush.services.yml file:
services: custom_helper.commands: class: \Drupal\custom_helper\Commands\VgwortRunAsAdmin arguments: ['@account_switcher'] tags: - { name: drush.command }
- π©πͺGermany osopolar π©πͺ GER π
@alexpott Do you have an idea how to fix it? Should there be a configuration where the user id for rendering might be set?
- π©πͺGermany osopolar π©πͺ GER π
I now want to use cron to send the registration messages to vg wort, but do not want to mess with cron, ie. to run the complete cron job as admin. I looked how search api renders the content for "rendered_item" field. It allows to define a role to be used for that field, see \Drupal\search_api\Plugin\search_api\processor\RenderedItem::addFieldValues(). There it uses the account switcher:
// Change the current user to our dummy implementation to ensure we are // using the configured roles. $this->getAccountSwitcher() ->switchTo(new UserSession(['roles' => array_values($roles)])); // ... render content ... // Restore the original user. $this->getAccountSwitcher()->switchBack();
Could we implement similar logic for vg wort?
- Merge request !50Render content for registration message text using specific roles β (Open) created by osopolar
- π©πͺGermany osopolar π©πͺ GER π
@alexpott What do you think about the solution? Search API also switches the theme. I think that might be considered as well to ensure that correct theme is used for rendering. For example, I set roles for rendering to "authenticated user", which does not have the right to use the admin theme. Calling /node/{nid}/vgwort now showed up as default theme instead of admin theme. I assume the default theme was used for generating the message text and drupal didn't switch back to admin theme later. If the role "authenticated user" has the right to use the admin theme, everything works as expected. That made me think that different themes might be used to render the content which may leads to different results. Ensuring that i.e. the default theme is always uses makes it more consistent. Any thoughts on this?
- π©πͺGermany osopolar π©πͺ GER π
Might be related to π¬ How to render message text if node view is disabled for anonymous user? Active .