Score:-1

undefined drupal_add_js function inside themename_preprocess_page

cn flag

I wanted to use javascript to delete an option from the list of options in case the user is not an administrator and I searched and found this code inside a post and I used it inside the project's themes->mythemename->mythemename.theme file, but that didn't work and I have several problems I want to know how to solve, as I searched a lot I did not find a solution

function mythemename_preprocess_page(&$variables, $hook) {
    if (!in_array("administrator", \Drupal::currentUser()->roles) {
        if (isset($variables['node']->type) && !empty($variables['node']->type) && (strpos($variables['node']->type, 'proxy_request') !== false)) {
            drupal_add_js(drupal_get_path('theme', 'mythemename') . '/js/datcbaseauth.js');
        }
    }
}

The first problem is that I can't get the data of the current user, as I am always reported that it is the second parameter of the in_array function is null.

The second problem is that I am telling that the second parameter of the strpos function is an object and not just a string even though the example in the post was treating it as just a string.

The third and final problem is that I am saying that function drupal_add_js and function drupal_get_path are undefined.

I searched for a long time, but I did not find a solution to these 3 problems, even the topics they may mentioned in it are not complete, for example, do not report how to use the drupal_add_js function inside the .theme file what is the prerequisite or even within Drupal's documentation itself, there is no data on the variable &$variables will tell about its content, the nature of each part, etc except that it is an associative array

Score:1
gb flag
  • First problem to get current user roles use this

    \Drupal::currentUser()->getRoles();

  • Second problem: $variables['node']->type is an array so use $variables['node']->bundle() to get type instead:

  • The third drupal_add_js not supported anymore in Drupal 8 use library and attached so try something like the following


/**
 * Implements hook_preprocess_HOOK().
 */
function THEME_preprocess_page(&$variables) {
  if (in_array("administrator", \Drupal::currentUser()->getRoles())) {
    if ($variables['node']->bundle() && (strpos($variables['node']->bundle(), 'proxy_request') !== FALSE)) {
      $variables['#attached']['library'][] = 'THEME/custom-library';
    }
  }
}

Amr.Mohammad87 avatar
cn flag
I got this error after editing "Error: call to a member function bundle or null in gasfv1_preprocess_page()" knowing that the name gasfv1 is the name of the custom theme. This is my new code now function -> function gasfv1_preprocess_page(&$variables, $hook) { if (!in_array("administrator", \Drupal::currentUser()->getRoles()) { if ($variables['node']->bundle() && (strpos($variables['node']->bundle(), 'proxy_request') !== false)) { $variables['#attached']['library'][] ='gasfv1/js/datcbaseauth.js'; } } }
Amr.Mohammad87 avatar
cn flag
Is my question that difficult?
berramou avatar
gb flag
if you get the error add the the condition *isset($variables['node'])* in the if statement!
Amr.Mohammad87 avatar
cn flag
The problem is not in the condition. The problem is that it means that there is no item called a node inside the variable $variables and I want to know if the machine name of the content type being rendered contains a certain pattern, i.e., proxy_request, so that I can insert my javascript file for those pages, which are content type
berramou avatar
gb flag
Normally the **$variables['node']** contain node information if you are in node page, for example you have a content type called *proxy_request* and your current page is node of type *proxy_request you will find the informations inside node key
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.