Add option to display the login button when user is not logged in

Created on 4 June 2025, 4 months ago

Problem/Motivation

The Add button doesn't show if user has not the permission to add a node in the bundle, and this is OK. It would be interesting to display a login button "login to add a " instead, in the way the forum module does it.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

✨ Feature request
Status

Active

Version

1.2

Component

Code

Created by

πŸ‡«πŸ‡·France erwangel

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

Merge Requests

Comments & Activities

  • Issue created by @erwangel
  • πŸ‡«πŸ‡·France erwangel

    This was pretty easy to do. Here is the code if someone wants to make a patch. Just put this in the render function before the $element array. For a full solution, we'll also have to create a new field 'login_label' in the settings form to hold the 'Login to add your @bundle_label' chain int the way the 'Add a new entry' is done in defineOptions() and buildOptionsForm(). For those who can't wait for a patch, the same can be achieved in hook_preprocess_views_view: filter on $variables['display_id'] and work the $variables['header']['add_content_by_bundle'] array.

        // Start new code
        if (!$access) {
           $bundle_info = \Drupal::service("entity_type.bundle.info")->getBundleInfo($entity_type->id());
           $bundle_label = $bundle_info[$bundle_type]['label'];
           $this->options['label'] = $this->t('Login to add your @bundle_label', ['@bundle_label' => $bundle_label]);
           $url = new Url('user.login', [], ['query' => $params]);
           $access = $this->accessManager->checkNamedRoute('user.login', [], $account);
        }
        // End of new code
        // Assemble elements into a link render array.
        $element = [
          '#type' => 'link',
          '#title' => $this->options['label'],
          '#url' => $url,
          '#options' => [
            'attributes' => ['class' => $classes],
          ],
          '#access' => $access,
        ];
    
  • Merge request !9Issue #3528444 - Add log in button β†’ (Open) created by AstonVictor
  • Pipeline finished with Success
    6 days ago
    Total: 331s
    #619265
Production build 0.71.5 2024