Tag Archives: Drupal Development

Drupal 7 – Get path alias by Node ID or Term ID

We did talk about using drupal_lookup_path() to get path alias.

The above function is still working in Drupal 7. We could also use the url() to get the path alias url as well.

// Get the node id path alias
url('node/' . $tid);

// Get the term id path alias
url('taxonomy/term/' . $tid);

 

Done =)

Reference:

Drupal 7 – Check add or edit in node form alter

If we want to alter a node form of a specific content type using hook_form_alter(), we may want to know whether the form is for node creation or edit. In this case, we can add a checking as follow:

function <module>_form_<content-type>_node_form_alter(&$form, &$form_state, $form_id) {
  $node = $form_state['node'];
  
  if (isset($node ->nid)) {
    // Node edit
  } else {
    // Node add
  }
}

 

Done =)

Drupal 7 – Alter the module execution order

Sometimes you may have 2 different modules implementing hook_form_alter() on the same form. In that case, the order of the module execution would follow the alphabetic order of the module name. If you want to alter the execution order, you could either update the system table in database or install the Modules Weight module.

1. Download and install the Modules Weight.
drupal7-module-execution-order-1
 

2. Go to admin/config/system/modules-weight to adjust the module weight.
drupal7-module-execution-order-2
 

Done =)

Reference:

Drupal 7 – The form has become outdated

After enabling the Cache blocks option @ admin/config/development/performance, the form inside a custom block throws the following error.

  • The form has become outdated. Copy any unsaved work in the form below and then reload this page.

 

One work around is to disable cache for that specific custom block and this could be done by adding the DRUPAL_NO_CACHE in your hook_block_info().
Continue reading Drupal 7 – The form has become outdated

Drupal 8 – Create a node through RESTful Web Service @ 1

In the previous posts, we have gone through some examples about working with Drupal 8 RESTful web service.

 

We can also add node to Drupal through web service. But as i mentioned before, the RESTful feature is not fully completed so we can only create a node with a title. Here is a example web service client in PHP using Guzzle.
Continue reading Drupal 8 – Create a node through RESTful Web Service @ 1

Drupal 8 – Login in Guzzle Web Service Client @ 1

1. Get your PHP client ready by following my previous post.

 

2. Update your index.php as follow.

<?php
  require 'vendor/autoload.php';
  use Guzzle\Http\Client;
  use Guzzle\Plugin\Cookie\CookiePlugin;
  use Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar;

  $cookiePlugin = new CookiePlugin(new ArrayCookieJar());
  $client = new Client('http://drupal8.localhost.com');
  
  $client->addSubscriber($cookiePlugin); 
  $request = $client->post('user', null, array(
    'name' => '<username>',
    'pass' => '<password>',
    'form_id' => 'user_login_form',
  ));
  $request->addHeader('Accept', 'application/json');

  $response = $request->send()->json();

  print '<pre>';
  print_r($response);
  print '</pre>';
?>

Continue reading Drupal 8 – Login in Guzzle Web Service Client @ 1

Connect Drupal 8 RESTful Service with Guzzle PHP Web Service Client @ 1

Previously I have setup a a Drupal 8 which is RESTful ready.

Guzzle is a PHP HTTP client & framework for building RESTful web service clients. In this example, we would create a simple .php to fetch a node from Drupal 8.

1. Make sure you have the Composer installed. You can refer to my previous post.

Continue reading Connect Drupal 8 RESTful Service with Guzzle PHP Web Service Client @ 1

Drupal 8 – Setup the RESTful Service @ 1

After studying Drupal 8 for a few days, i got the feeling that there are still a lot of works to polish it before a beta or RC release. And lacking documentation is another big problem to kick start with Drupal 8. This post may help you to get some simple idea on how to setup RESTful service in Drupal 8 but very likely, the details will change in the future.

1. Enable the RESTful Web Services.
drupal-8-setup-rest-service-1
Continue reading Drupal 8 – Setup the RESTful Service @ 1