7

Hi i am using Drupal 8 to render a modal popup on clicking a link. However when i am connected as admin the popup render well but as soon as i logout or choose to be incognito mode of the browser the popup does not appear and the page is redirected to link set in the href tag. Here is my link :

<a href="/mysite.com/contact/download_file_advance?nid=371" class="use-ajax" data-dialog-type="modal" data-accepts="application/vnd.drupal-modal" data-filename="Livre Blanc">SOME TEXT IN MODAL</a>

Can anyone explain to me why this is so? I can't figure out why when i am connected the modal render well but when not connected it redirect me to the node.

  • have you checked your permissions. – vijay Mar 27 '17 at 7:38
  • @vijay Where can i chek the permission? – EXE Mar 27 '17 at 7:44
  • check it in admin/modules there will be options like configuration, permissions, etc., for each module – vijay Mar 27 '17 at 7:50
  • @vijay I am not using any module to display the modal...Infact am using the core module modal provided by drupal 8 itself. I have created a user having role as Authenticated User only but when login using this user also the modal does not appear. – EXE Mar 27 '17 at 7:55
  • I think anonymous user is missing the modal library. Either use proper render arrays or add the library via hook_page_attachments – user21641 Mar 27 '17 at 8:34
7

Ajay is correct, the javascript dependencies wont load by default for anonymous users. Here's what I added to my theme.libraries.yml to get it working:

dependencies: 
  - core/jquery
  - core/drupal.dialog.ajax
| improve this answer | |
  • I think not just don't load for anonymous; I think they don't load for any non-administrator role. – liquidcms Apr 11 '19 at 17:52
  • This is working for logged in users but not for everyone – Ekta Puri Jul 19 '19 at 1:44
2

For me the only thing that helped beside of hacking PHP code, which in my opinion would be an overkill, was to add the following to the MY_THEME.info.yml under the section libraries:

- 'core/drupal.dialog.ajax'

In my case, using a bootstrap based theme, the whole section looked like this:

libraries:
  - 'bootstrap_freigeist/global-styling'
  - 'bootstrap_freigeist/bootstrap-scripts'
  - 'core/drupal.dialog.ajax'

Be aware that adding the script to the MY_THEME.libraries.yml didn't do the trick for me.

| improve this answer | |
1

The answers posted here are right, also you can include jQuery libraries and the Ajax libraries using hook_page_attachments, just implement this function in your yourmodule.module file, replacing the name of your module accordingly, of course:

function yourmodule_page_attachments(array &$attachments) {
    $attachments['#attached']['library'][] = 'core/jquery';
    $attachments['#attached']['library'][] = 'core/drupal.dialog.ajax';
}

This snippet will add jQuery and Ajax required libraries to all your pages, then you can write your own logic in it if you want to restrict this libraries to only certain pages.

Good luck!

| improve this answer | |
1

For me adding dependencies in libraries.yml did not worked, I am using drupal 8.x version. Below is working solution for me

In your theme file, say your theme name is "abc" then you will have a file "abc.theme", add following code in file. assuming your page alias path is "abcde" where you want to add the dialog box

function abc_page_attachments_alter(&$page){
        $current_path = \Drupal::service('path.current')->getPath();
        $path = $current_path = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);
        if($path == '/abcde') {
            $page['#attached']['library'][] = 'core/drupal.ajax';
        }
    }

if you want to load library in all pages then replace above function with

  function abc_page_attachments_alter(&$page){
                $page['#attached']['library'][] = 'core/drupal.ajax';
        }

clear cache and hard refresh your page. dialog box should be working now

| improve this answer | |
0

You need to add code dependencies in your theme for ajax and jQuery in your theme.libraries.yml file.

Drupal by default does not load ajax for anonymous users.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.