Drupal 7: drupal_add_js() is not adding .js file - drupal

I want to add a .js file to my site and have it appear in the <head>
To do this I added the following to the theme's template.php file:
function mytheme_preprocess_page(&$variables) {
drupal_add_js(drupal_get_path('theme', 'mytheme') . '/myfile.js', array(
'type' => 'file',
'group' => JS_THEME,
'scope' => 'header',
));
}
But nothing appears when I check the source code (note the .js file is in the theme root directory).
I also tried
drupal_add_js('jQuery(document).ready(function () { console.log("Hello!"); });', 'inline');
But got nothing.
Would anyone know why this is occuring and what I could do?

Related

make user upload image to in theme folder

i am developing a wordpress theme. i want to make my users' images saved in NOT https://example.com/wp-content/uploads folder, but in https://example.com/wp-content/themes/mytheme/images folder.
i added this code to my themes' functions.php file :
function my_custom_upload_dir( $dir ) {
return array(
'path' => WP_CONTENT_DIR . '/portfolio/images',
'url' => WP_CONTENT_URL . '/portfolio/images',
'subdir' => '/portfolio/images',
) + $dir;
}
add_filter( 'upload_dir', 'my_custom_upload_dir' );
and i got this error :
Unable to create directory wp-content/uploads/portfolio/images. Is its parent directory writable by the server?
i dont want to save in wp-content/uploads/portfolio/images file, i want to save in wp-contect/themes/mytheme/images file. how can i solve this problem?

Drupal: Module output not appearing on front end

I am trying to create a Drupal module. I've been able to setup the configuration form on the admin section - it runs fine: I can add the component and set configurations and save.
However, nothing appears on the front end of the site. There are no errors. I'm not sure why and, as I am new to Drupal, I'm not sure where to look.
My hook_theme in my .module file looks like:
function gallery_grid_theme($existing, $type, $theme, $path) {
return array(
'gallery_grid' => array(
'template' => 'gallery-grid',
'path' => 'plugins/content_types/gallery_grid/templates',
'type' => 'theme',
'variables' => [],
)
);
}
The .tpl file is intact and has no markup errors.
Would anyone know what file I should be looking at?
EDIT:
I've tried clearing the cache and rebuilding the registry as well as disabling and re-enabling the module, to no affect.
The module is added to a page panel as a component (gear icon, Add Content).
For some reasons this needed an absolute path:
'path' => drupal_get_path('module', 'gallery_grid') . '/plugins/content_types/gallery_grid/templates',

Add js to a template file using theme preprocess

I have a custom Drupal 7 module which is hooking into the media module and I'm having trouble with adding some js into its render.
The module is defining a theme like this:
function media_flash_theme($existing, $type, $theme, $path) {
return array(
'media_flash_video' => array(
'variables' => array('uri' => NULL, 'options' => array()),
'file' => 'media_flash.theme.inc',
'path' => $path . '/includes/themes',
'template' => 'media-flash',
)
);
}
And my formatter (view) is returning my element like so:
$element = array(
'#theme' => 'media_flash_video',
'#uri' => $file->uri,
'#options' => array(),
);
return $element;
Now, I have a preprocess function which adds some js:
function media_flash_preprocess_media_flash_video(&$variables) {
$path = libraries_get_path('swfobject');
drupal_add_js($path . '/swfobject.js');
...
}
And also a drupal add js in my template file:
/**
* #file media_flash/includes/themes/media-flash.tpl.php
*/
$javascript = '
(function ($) {
...
})(jQuery);
';
drupal_add_js($javascript, 'inline');
The issue is weird. When I'm logged in then everything works fine, all the time. However, when I am using it as an anonymous user it all works fine the first load (after cache clear) but then the two javascripts arent added anymore.
I have tried to change my preprocess function so it adds the javascript with this $variables['scripts'] = drupal_get_js(); but this also has the same behaviour.
Ive googled around a bit and found some suggestions but nothing's worked thus far. Any help is appreciated.
Thanks,
EDIT: So I looked into this a bit more and the code is being executed through the filter module. It seems as though the first time it gets executed it gets cached and then it is just recieved from the cache each time after that, so the drupal_add_js code isn't run again.
Is there a way around this caching or do I need to remove my js from this part all together?
You can always add the js in the page level "hook_page_alter(&$page)"

How do I properly add FILE field in drupal form via theme-setting.php?

I am building a theme with ability to upload custom background images but now I am stuck at a point.
How do I properly add FILE field in drupal form via theme-setting.php and after that how can I get public url to this file in my template files??
In your theme_form_system_theme_settings_alter hook you need to add the following form element:
$form['theme_settings']['background_file'] = array(
'#type' => 'managed_file',
'#title' => t('Background'),
'#required' => FALSE,
'#upload_location' => file_default_scheme() . '://theme/backgrounds/',
'#default_value' => theme_get_setting('background_file'),
'#upload_validators' => array(
'file_validate_extensions' => array('gif png jpg jpeg'),
),
);
This will save the file id to your theme settigns variable 'background_file', notice that i set the upload location to theme/backgrounds, this will be inside your files folder.
Finally you'll get the complete URL to the file with file_create_url:
$fid = theme_get_setting('background_file');
$image_url = file_create_url(file_load($fid)->uri);
Edit:
In your template.php you can add in the theme_preprocess_page hook the variable so all the tpl's can access it, this is how:
function theme_preprocess_page(&$variables, $hook) {
$fid = theme_get_setting('background_file');
$variables['background_url'] = file_create_url(file_load($fid)->uri);
}
Hope this helps! :D

Drupal: Passing custom variable from custom module to my template

I realize this question has been asked, but I either simply don't understand or the previous answers don't apply (or I don't understand how to apply them) to my circumstance. Here goes:
I have a custom module named:
"my_module" in /sites/all/modules/custom/my_module
I have a module file:
/sites/all/modules/custom/my_module/my_module.module
I have a page template name "page-mypage" which is NOT in my module:
/sites/all/themes/mytheme/pages/page-mypath-mypage.tpl.php
I made the hook menu for this:
$items['mypath/mypage'] = array(
'title' => 'My Page!',
'page callback' => 'my_module_mypage',
'page arguments' => array(1,2),
'access callback' => true,
'type' => MENU_CALLBACK,
);
In the function, I build up some content like so:
function my_module_mypage($x, $y) {
$output = "foo AND bar!";
return $output;
}
In the template (again, NOT in my module folder, but in the THEME subfolder "pages", I have:
<?php print $content ?>
When I go to http://mysite/mypath/mypage I get "foo AND bar!"
Now for the question. I want a new variable, defined in my_module_mypage(), called '$moar_content'. I want to output $moar_content in my page-mypath-mypage.tpl.php. I only need to do this for this module and for this template. I do not need it theme-wide, so I don't think using mytheme's 'template.php' is appropriate.
I think I need to use some kind of preprocessing, but everything I try fails, and everything I read seems to be missing some kind of magic ingredient.
My thinking was:
function my_module_preprocess_page_mypath_mypage(&$variables) {
$variables['moar_content'] = 'OATMEAL';
}
or
function my_module_preprocess_my_module_mypage(&$variables) {
$variables['moar_content'] = 'OATMEAL';
}
or something. I'm pretty sure I'm on the right track, but I'm hitting a brick wall.
To do the job, you must follow Drupal's best practices, supposing you are using D6, so you can insert some variables to your template like this :
// You menu path is good
$items['mypath/mypage'] = array(
'title' => 'My Page!',
'page callback' => 'my_module_mypage',
'page arguments' => array(1,2),
'access callback' => true,
'type' => MENU_CALLBACK,
);
Second thing, we define the theme hook for our page
// We define here a new theme file for your your page
// Your theme file must be located in your module's folder
// you can use a subfolder to group all your module's theme files
// E.g : themes/my-module-theme.tpl.php
// Note that in theme files, we change _ by -
function my_module_theme() {
return array(
'my_module_theme' => array( // Keep that name in your mind
'template' => 'my_module_theme',
'arguments' => array(
'my_var' => NULL,
'my_var2' => NULL,
),
)
);
}
Now we can create a file "my-module-theme.tpl.php" in the root folder of our module, and paste something like "foo AND bar!"
Back to our my_module.module, the callback must be something like :
function my_module_mypage($x, $y) {
// $x and $y are optionnal, so this is the first manner
// to inject variables into your theme's file
$output = theme("my_module_theme", $x, $y);
return $output;
}
Also you can use preprocess hook to insert variables
// The hook must be named like this : template_preprocess_NAME_OF_THEME_FILE
// where NAME_OF_THEME_FILE is the name that you kept in your mind ;)
function template_preprocess_my_module_theme(&$variables) {
// Do some job
$var1 = 'Foobar';
// So in "my-module-theme.tpl.php", $my_var1 will print Foobar
$variables['my_var1'] = $var1;
}

Resources