Enabling hook form alter module - not showing up in modules - drupal

Good Day,
I am doing a hook_form_alter and followed a tutorial where you create a directory in your /sites/all/modules directory and place the module.info and mymodule.module files within. I've called that directory mymodule so the path will be /sites/all/modules/mymodule.
The problem I'm having is trying to enable the module. It does not show up in the list of Modules when I check. It's clearly in my directory.
My module (my_module.module) looks like this and is going to be used to modify the form where you add the product (basically taking out the SKU, list price and commission):
<?php
// $Id$
/**
* #file
* Module to hold my customizations to Ubercart
*/
/**
* Implementation of hook_form_alter()
*/
function my_module_form_alter($form_id, &$form) {
if ($form_id == 'product_node_form') {
$form['base']['model']['#required'] = FALSE;
$form['base']['prices']['list_price']['#required'] = FALSE;
$form['base']['prices']['cost']['#required'] = FALSE;
$form['base']['prices']['sell_price']['#required'] = FALSE;
$form['base']['prices']['shippable']['#required'] = FALSE;
}
}
And the info file, my_module.info, looks like this:
; $Id$
name = My module
description = My customizations to Ubercart
package = Custom
core = 7.x
Why would it not show up in the modules list and how do I make Drupal see the module so I can enable it?
I don't have a category called Custom and nothing is showing up.
So far, i've gone into my php.ini file and put in safe_mode=off to see if that would help (because someone said this sometimes causes Drupal to ignore new modules for some reason) but that didn't help.
I created both files on the server so I don't think there would be any issues with something like linefeeds (I use GoDaddy's FTP File Manager and it comes with an editor.)
What could the problem be because I'm stumped?
Thanks for looking.

Make sure to name the .info and .module files with the same name.
E.g. mymodule.info and mymodule.module.
Note that, the name should be alpha-numeric (underscore is allowed) and starts with a letter.
In your hook implementation, replace the word hook with your module's name
function mymodule_form_alter($form, &$form_state, $form_id)
{
// code goes here...
}

I discovered something that works. After reading about people having issues with linefeeds and not knowing if this was creating the problem, I decided to try something.
I went to a module directory and found a .info file. In my case, I went to a directory that had Commerce in it and copied the Commerce.info file to my custom module's directory.
Then, what I did was take out the stuff that Drupal added after it installed the Commerce module (you can do the same for whatever .info file you copy from) and modified the information at the start, removing any dependency lines or anything that doesn't matter. In other words, make it like the .info file you are trying to use but DON'T COPY from the .info you've been trying to enable!
Save it this file (in my case, it's still commerce.info.)
Now delete the .info file you were trying to enable originally.
Rename the new .info file to the name you wanted (commerce.info => mymodule.info.)
Go to your Modules page and refresh it.
Voila...there it is. Enable and save configuration.

Related

WordPress plugin localization, load_plugin_textdomain doesn't work

Ok i built plugin for contact form, I wanna add translation for it. In my main plugin files i add this code
function ap_action_init() {
// Localization
load_plugin_textdomain('prijava_forma', false, dirname(plugin_basename(__FILE__))."/languages";
}
// Add actions
add_action('init', 'ap_action_init');
in my file where contact form is written i have this
_e( 'Prva','prijava_forma' );
In my language folder I added the .mo and .po files created with Poedit.
Also I defined WPLANG in config.php and changed the language in the dashboard.
But i get no translation. Where could be problem, i am new to this?
There are many possible causes:
.mo file not readable or not found at all (.po file is not used by WordPress by the way)
The string you are expecting is not translated yet
Wrong .mo file name, valid names are ar.mo, fr_FR.mo..., invalid ones are br_BR.mo, arabic.po, AR.mo, ar_AR.mo... So make sure you get this one right.
for plugins the name will be the concatenation for the text domain, a dash and the locale: myplugin-ru_RU.mo
Check what load_plugin_textdomain() returned, if the .mo file was loaded, it should return true, in which case the next step would be to check that you are not missing the textdomain parameter in your __(), _e() and similar functions.
More on WordPress localization
It may be also caused on hook where the functions is attached and where are the po/mo files.
On the Init Hooks, load_plugin_textdomain() returned true but string were not translated.
I changed the action to plugins_loaded as my po/mo were in a folder inside a Custom plugins.
Also make sure that your string stands for itself. Do not append anything to the string, do this after the gettext function instead.
Wrong:
return __('Please translate me'.'(666)','your-textdomain');
Right:
return __('Please translate me','your-textdomain').'(666)';

Drupal 6 preprocess function for custom template

I have a custom template file I'm using for a page.
Say the page is www.mydomain.com/hello-world
The content of this page is taken from a template file called hello-world.tpl.php
Now I need to have a variable prepared in advance for that page, so I took a look at how core modules achieve this and tried to implement in the same way, only the variables are always null..
for hello-world.tpl.php I created in my module file a function called:
function template_preprocess_hello_world(&$variables) {
$variables['test'] = "test";
}
As I said $test doesn't get any value on www.mydomain.com/hello-world (I receive NULL when var dumping it)
I spent an hour or so double checking that I don't have any typos or anything like that.
Is what I'm doing worng??
Try renaming the function to mymodulename_preprocess_hello_world(&$variables). Don't forget to clear the cache as well.

Laravel/blade caching css files

I am working on Nginx server, with PHP-FPM. I installed Laravel 4.1 and bootstrap v3.1.1., and here is the problem. For the last 30 minutes, I have been trying to change a css rule that I first declared to check boostrap.
.jumbotron{
background: red;
}
The first time it worked. The jumbotron container was red. So, I removed that css value and started working, but still no matter which browse I use, the container is red. I even checked the css file through the Google Chromes inspection tool, and it is showing me that first value when jumbotron had a background:red. I deleted the css file and renamed it and add new styles, I configured chrome not to cache pages. But Still the same value. I'm convinced now, that Laravel has kept a cache of the first style declaration.
Is there any way to disable this at all?
General explanation
When you access a Laravel Blade view, it will generate it to a temporary file so it doesn't have to process the Blade syntax every time you access to a view. These files are stored in app/storage/view with a filename that is the MD5 hash of the file path.
Usually when you change a view, Laravel regenerate these files automatically at the next view access and everything goes on. This is done by comparing the modification times of the generated file and the view's source file through the filemtime() function. Probably in your case there was a problem and the temporary file wasn't regenerated. In this case, you have to delete these files, so they can be regenerated. It doesn't harm anything, because they are autogenerated from your views and can be regenerated anytime. They are only for cache purposes.
Normally, they should be refreshed automatically, but you can delete these files anytime if they get stuck and you have problems like these, but as I said these should be just rare exceptions.
Code break down
All the following codes are from laravel/framerok/src/Illuminate/View/. I added some extra comments to the originals.
Get view
Starting from Engines/CompilerEngine.php we have the main code we need to understand the mechanics.
public function get($path, array $data = array())
{
// Push the path to the stack of the last compiled templates.
$this->lastCompiled[] = $path;
// If this given view has expired, which means it has simply been edited since
// it was last compiled, we will re-compile the views so we can evaluate a
// fresh copy of the view. We'll pass the compiler the path of the view.
if ($this->compiler->isExpired($path))
{
$this->compiler->compile($path);
}
// Return the MD5 hash of the path concatenated
// to the app's view storage folder path.
$compiled = $this->compiler->getCompiledPath($path);
// Once we have the path to the compiled file, we will evaluate the paths with
// typical PHP just like any other templates. We also keep a stack of views
// which have been rendered for right exception messages to be generated.
$results = $this->evaluatePath($compiled, $data);
// Remove last compiled path.
array_pop($this->lastCompiled);
return $results;
}
Check if regeneration required
This will be done in Compilers/Compiler.php. This is an important function. Depending on the result it will be decided whether the view should be recompiled. If this returns false instead of true that can be a reason for views not being regenerated.
public function isExpired($path)
{
$compiled = $this->getCompiledPath($path);
// If the compiled file doesn't exist we will indicate that the view is expired
// so that it can be re-compiled. Else, we will verify the last modification
// of the views is less than the modification times of the compiled views.
if ( ! $this->cachePath || ! $this->files->exists($compiled))
{
return true;
}
$lastModified = $this->files->lastModified($path);
return $lastModified >= $this->files->lastModified($compiled);
}
Regenerate view
If the view is expired it will be regenerated. In Compilers\BladeCompiler.php we see that the compiler will loop through all Blade keywords and finally give back a string that contains the compiled PHP code. Then it will check if the view storage path is set and save the file there with a filename that is the MD5 hash of the view's filename.
public function compile($path)
{
$contents = $this->compileString($this->files->get($path));
if ( ! is_null($this->cachePath))
{
$this->files->put($this->getCompiledPath($path), $contents);
}
}
Evaluate
Finally in Engines/PhpEngine.php the view is evaluated. It imports the data passed to the view with extract() and include the file with the passed path in a try and catch all exceptions with handleViewException() that throws the exception again. There are some output buffering too.
Same issue here. I am using VirtualBox with Shared Folders pointing to my document root.
This pointed me in the right direction:
https://stackoverflow.com/a/26583609/1036602
Which led me to this:
http://www.danhart.co.uk/blog/vagrant-virtualbox-modified-files-not-updating-via-nginx-apache
and this:
https://forums.virtualbox.org/viewtopic.php?f=1&t=24905
If you're mounting your local dev root via vboxsf Shared Folders, set EnableSendFile Off in your apache2.conf (or sendfile off if using Nginx).
For what it's worth and because this answer came up first in my google search...
I had the same problem. The CSS and JS files wouldn't update. Deleting the cache files didn't work. The timestamps were not the problem. The only way I could update them was to change the filename, load it directly to get the 404 error, and then change the name back to the original name.
In the end the problem was not related to Laravel or the browser cache at all. The problem was due to NginX using sendfile which doesn't work with remote file systems. In my case, I was using VirtualBox for the OS and the remote file system was vboxsf through Guest Additions.
I hope this saves someone else some time.
In Laravel 5.8+ you can use so:
The version method will automatically append a unique hash to the filenames of all compiled files, allowing for more convenient cache busting:
mix.js('resources/js/app.js', 'public/js').version();
After generating the versioned file, you won't know the exact file name. So, you should use Laravel's global mix function within your views to load the appropriately hashed asset. The mix function will automatically determine the current name of the hashed file:
<script src="{{ mix('/js/app.js') }}"></script>
full document: https://laravel.com/docs/5.8/mix

Set PHP Variables for Drupal 7 Theme Files

I want to set custom php variables that can be used throughout my drupal theme (html.tpl.php, page.tpl.php etc.) I need the variables set based on the $title of the node. I'm not an expert on how Drupal works, the moduling and hooks, and I just need to know the simplest/easiest way to accomplish this. I keep reading about implementing some sort of hook in the template.php file where you can set variables, but I've been unsuccesful with everything I've tried.
So, basically, how would you accomplish this:
Get $title of Node
Set variables that will be passed along into theme files (for example, to do basic things like: if($title == 'news_page') { $siteSection = "news"; } )
Have $siteSection be available to use in theme files
Any help would be great.. thanks!
Before Drupal builds the HTML for a page from a theme's template (.tpl.php file), it runs preprocess "hooks". Hooks are basically a naming convention for functions that let modules and themes override or "hook" onto Drupal core processes.
E.g., if you want to display a message to a user when they log in, you can use hook_user_login.
function MODULENAME_user_login(&$edit, $account) {
drupal_set_message("Welcome, ". $account->name);
}
When a user logs in, Drupal looks for all loaded functions that end in "_user_login" and it runs them. If this function is in an enabled module, it has been loaded, so it will get run as well.
If you want to make a variable named $site_section available in your page.tpl.php file, you can hook into template_preprocess_page. This is a theme hook, so the name is a little different, but it functions pretty much the same way. To call this hook from your theme, you need to create a file called template.php in your theme's directory. Inside template.php, we'll add:
<?php
function THEMENAME_preprocess_page(&$vars){
switch (drupal_strtolower($vars['node']->title)) {
case "about page":
$site_section = "about";
break;
case "news page":
case "news page1":
case "news page2":
$site_section = "news";
break;
default:
$site_section = "none";
break;
}
$vars['site_section'] = $site_section;
}
The <?php is used to tell the server the treat all of the proceeding code as PHP. We then declare our hook function with the intention of loading Drupal's array of page variables into a local variable called $vars. By adding the & before $vars, we'll be allowed to modify the values for use outside of this function.
The switch statement will let us efficiently test the page title for multiple values. The value of the node's title may contain uppercase letters, lowercase letters, and symbols, so to avoid a case-sensitive mismatch, we're going to convert the title to lowercase and only test that (symbols will still be in the title, though). After the switch statement, we set the value of our $site_section local value into the referenced $vars array for use in page.tpl.php.
However, if it's just your intention to break the site up into sections for theming purposes, there are other ways of accomplishing that. My answer to a similar situation a few months ago might be helpful.

Saving nodes with a filefield

I'm in the progress of creating a bulk upload function for a Drupal site. Using flash I'm able to upload the files to a specific url that then handles the files. What I want to do, is not just to upload the files, but create a node of a specific type with the file saved to a filefield that has been setup with CCK. Since these are audio files, it's important that filefield handles the files, so addition meta data can be provided with the getid3 module.
Now I've looked through some of the code as I wasn't able to find an API documentation, but it's not clear at all how I should handle this. Ideally I could just pass the file to a function and just use the data returned when saving the node, but I haven't been able to find that function.
If any one has experience with this I would apreciate some pointers on how to approach this matter.
I had to do something similar some weeks ago and ended up adapting some functionality from the Remote File module, especially the remote_file_cck_attach_file() function. It uses the field_file_save_file() function from the filefield module, which might be the function you're looking for.
In my case, the files are fetched from several remote locations and stored temporarily using file_save_data(). Attaching them to a CCK filefield happens on hook_nodeapi() presave, using the following:
public static function attachAsCCKField(&$node, $filepath, $fieldname, $index=0) {
// Grab the filefield definition
$field = content_fields($fieldname, $node->type);
$validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field));
$fieldFileDirectory = filefield_widget_file_path($field);
// This path does not necessarily exist already, so make sure it is available
self::verifyPath($fieldFileDirectory);
$file = field_file_save_file($filepath, $validators, $fieldFileDirectory);
// Is the CCK field array already available in the node object?
if (!is_array($node->$fieldname)) {
// No, add a stub
$node->$fieldname=array();
}
$node->{$fieldname}[$index] = $file;
}
$filepath is the path to the file that should be attached, $fieldname is the internal name of the filefield instance to use within the node and $index would be the 0 based index of the attached file in case of multiple field entries.
The function ended up within a utility class, hence the class syntax for the verifyPath() call. The call just ensures that the target directory is available:
public static function verifyPath($path) {
if (!file_check_directory($path, FILE_CREATE_DIRECTORY)) {
throw new RuntimeException('The path "' . $path . '" is not valid (not creatable, not writeable?).');
}
}
That did it for me - everything else happens on node saving automatically.
I have not used the getid3 module yet, so I have no idea if it would play along with this way of doing it. Also, I had no need to add additional information/attributes to the filefield, so maybe you'd have to put some more information into the field array than just the file returned by field_file_save_file(). Anyways, hope this helps and good luck.
I have done something whith imagefield which worked, I think the structure has to be right otherwise it won't work. It took a lot of trial and error. This is is what I populated the imagefield with.
$image['data'] =array(
'title' => $media_reference_attributes->getNamedItem("source")->value,
'description' => $description,
'alt' => "",);
$image['width'] = $width;
$image['height'] = $height;
$image['mimetype'] = $mime_type
$image['uid'] = 1;
$image['status'] = 1;
$image['fid'] = $fid;
$image['filesize'] = $file->filesize;
$image['nid'] = $id;
$image['filename'] = $url;
$image['timestamp'] = $file->timestamp;
$image['filepath'] = $file_path;
Hope this is of some help.
You might want to look at Image FUpload if you need a look at integrating the flash upload.
To push the files on to another server while still handling them through Drupal sounds a little like the CDN space, maybe look at the behavior in the CDN or CDN2 projects?
If you find a clear solution please come back and post it!

Resources