Tag Archives: Drupal 7

Drupal 7 – Get taxonomy terms by EntityFieldQuery

The EntityFieldQuery is not limited to node, it can be used to retrieve any entity types in Drupal 7. Here comes an example about getting taxonomy terms with EntityFieldQuery.

I would like to retrieve all terms with of a specific vocabulary which has vid = 1 with the descending weight order.
Continue reading Drupal 7 – Get taxonomy terms by EntityFieldQuery

Drupal 7 – Get mulitple nodes using EntityFieldQuery and node_load_multiple()

We tried to get specific nodes using EntityFieldQuery.
Drupal 7 – Get specific nodes using EntityFieldQuery

It returns a list of node ids which fulfill the query conditions. The next step is usually the retrieval of the nodes by the node ids. This can be done by the following example using node_load_multiple().
Continue reading Drupal 7 – Get mulitple nodes using EntityFieldQuery and node_load_multiple()

Drupal 7 – Customize the user login block

In Drupal 7, there is a user login block. We can customize that block using the hook_form_FORM_ID_alter(). You can create a custom module or implement it directly in the theme template.php.

Here is a simple example written by Liam McDermott. You can add this in the template.php and replace THEMENAME by your theme name.
Continue reading Drupal 7 – Customize the user login block

Drupal 7 – form_set_error does not show

I have a custom form and i render the form using drupal_get_form() inside the .tpl.php. One day, i find that when the submission could not pass the validation, only the failed field was highlighted but without the error message. Then i find the following post suggesting that i should called the drupal_get_form() in the .module file or preprocess function instead of the .tpl.php.
form_set_error Not displaying error after invalid form submission

So i put the drupal_get_form() call in the preprocess function and set the form in the $vars so it could be printed in the .tpl.php. But that still could not solve the problem. Finally i come up with a simple solution which is just adding the following line in the .tpl.php.

<div><?php print theme_status_messages(array('display' => 'error')); ?></div>

 

Done =)

Reference:

Drupal 7 – Get the relative and absolute public file path

Previously we talked about how to get the absolute file path of your Drupal installation.
Drupal – Get the absolute file path of your Drupal installation

There are also other ways to get different file paths in Drupal. Suppose the Drupal webroot path is /var/www/drupal.

print variable_get('file_public_path', conf_path() . '/files');
// output: sites/default/files 

print drupal_realpath(file_default_scheme() . '://');
// output: /var/www/drupal/sites/default/files

print realpath(".");
// output: /var/www/drupal

 

Done =)

BeansTag – Override existing meta tag if it already exists

Originally, BeansTag will check if there is any existing meta tags in the page which is going to be rendered. If exists, the BeansTag module will do nothing.

As requested by a BeansTag user, recently i have added a BeansTag settings page where you disable this checking.

 

If this option is checked. BeansTag will override the existing meta data.

Done =)

Reference: BeansTag – Override other metatag modules, possible?

Drupal 7 – Simple Ajax implementation @ 3

In the past 2 days we have developed a custom module which demonstrates the basic Ajax implementation in Drupal 7.

 

Today i will continue the example written by Kevin Hankens and make the module more secure. We want to add a token in the Ajax request such that the server could check if it comes from a valid user. This token could be obtained from drupal_get_token() which will return a fixed token value for each user session. Here are the list of changes in the new eureka_ajax.module.

  • Set the token value in Drupal.settings in hook_init()
  • Add the token as query string in the theme_ajax_link()
  • Check if the token is valid in ajax_request_callback()

Continue reading Drupal 7 – Simple Ajax implementation @ 3

Drupal 7 – Simple Ajax implementation @ 2

I will continue the Ajax example which we did in
Drupal 7 – Simple Ajax implementation @ 1

So this time we will add some access control on the above example. In Drupal 7 & 8 development, we could create new permissions by implementing the hook_permission(). After that we could modify the access arguments in the hook_menu().
Continue reading Drupal 7 – Simple Ajax implementation @ 2