How to render message text if node view is disabled for anonymous user?

Created on 1 March 2024, 10 months ago
Updated 6 August 2024, 5 months ago

Problem/Motivation

We are using a headless drupal and do not allow public access to our content. That's why the drush command vgwort:send and advancedqueue:queue:process do not render the plain message text as expected. I expect the same for cron job. What would you suggest to be able to render text anyway?

Steps to reproduce

Revoke permission for view node content from anonymous user and check the message text, i.e. with a breakpoint in \Drupal\vgwort\MessageGenerator::entityToNewMessage().

πŸ’¬ Support request
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany osopolar πŸ‡©πŸ‡ͺ GER 🌐

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @osopolar
  • πŸ‡©πŸ‡ͺGermany osopolar πŸ‡©πŸ‡ͺ GER 🌐
  • πŸ‡©πŸ‡ͺ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 🌐
  • πŸ‡©πŸ‡ͺGermany osopolar πŸ‡©πŸ‡ͺ GER 🌐
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    This is a great question and something we should fix. #3 is a good work around but it feels you should not have to.

  • πŸ‡©πŸ‡ͺ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?

  • Pipeline finished with Failed
    6 months ago
    Total: 176s
    #222311
  • Pipeline finished with Failed
    6 months ago
    Total: 268s
    #222325
  • πŸ‡©πŸ‡ͺ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 🌐
Production build 0.71.5 2024