PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites

27 Jul 2016

Pathauto added special characters in url alias - drupal 7

    To improve SEO, we need to clean our URLs. By default in drupal, we've an option called clean URLs at the configuration. In drupal 7, we can also manage the URLs. For instance, you have a content type called services. You wanted to each service page have url like services/page-name. To do that, we've a pathauto module in drupal 7. The pathauto module allow us to manage the URLs for every content types, files, taxonomy & users and also we can remove some unnecessary words from URL like an, the and so on.

   The pathauto module can remove some unnecessary words like a, an, the and so on & also remove special characters like !, @, $ and so on. Unfortunately, it doesn't included some other symbols like copyright(©), trademark(™), registered(®) and so on. But it provide a hook to add new symbols into the punctuation settings called hook_pathauto_punctuation_chars_alter. After created a content with some symbols which are represented above, your page URL looks like below image:


Drupal 7 - remove special characters from url using pathauto module



/**
 * Implements hook_pathauto_punctuation_chars_alter().
 */
function phponwebsites_pathauto_punctuation_chars_alter(array &$punctuation) {
  $punctuation['copyright']          = array('value' => '©', 'name' => t('Copyright'));
  $punctuation['registered']         = array('value' => '®', 'name' => t('Registered trademark'));
  $punctuation['trademark']          = array('value' => '™', 'name' => t('Trademark'));
}

   After implemented above code into your module, you cold see added symbols are listing on Pathauto module's settings page at /admin/config/search/path/settings. If You didn't get these symbols, clear cache & test it again. It looks like below image:


Drupal 7 - pathauto settings after hook_pathauto_punctuation_chars_alter


Now you can create a content with those symbols. The pathauto module didn't added those symbols into the URL.

Now I hope you know how to remove some special characters from URL alias using pathauto module in drupal 7.

28 Jun 2016

PHP get particular key value from multidimensional array

You can get specific key from multidimensional array using any one of the below methods:

  1. Get Specific key value form multidimensional array using array_column:


     The array_column() returns the values from a single column in the input array. It works only from PHP version 5.5. You've a multidimentional array with details of user name and country. You tried to get the name of every array from multidimentional array. Consider the following exampale:

  <?php

    $array = array(
      0 => array(
        'name' => 'Guru',
        'country' => 'India'
      ),
      1 => array(
        'name' => 'Clark',
        'country' => 'USA'
      ),
      2 => array(
        'name' => 'Smith',
        'country' => 'United Kingdom'
      ),
      3 => array(
        'name' => 'John',
        'country' => 'USA'
      ),
    );

    $namesArray = array_column($array, 'name');
    print_r($namesArray);exit;

    When you run this program in the browser, you will get the output like below one:

    Array ( [0] => Guru [1] => Clark [2] => Smith [3] => John )

    For more details about array_coulmn, visit http://php.net/manual/en/function.array-column.php.

  2. Get Specific key value form multidimensional array using array_map:


    The array_map() applies the callback to the elements of the given arrays. It works from PHP version 4.0.6. The array_map() is alternate of array_column() in PHP

  <?php
    $namesArray = array_map(function($arr){
        return $arr['name'];
      }, $array);
    print_r($namesArray);exit;

    It will give same output as array_column. For more details about array_map, visit http://php.net/manual/en/function.array-map.php.

  3. Get Specific key value form multidimensional array using foreach loop:


  <?php
  $namesArray = array();
  foreach($array as $key => $val) {
    $namesArray[] =  $val['name'];
  }
  print_r($namesArray);exit;

  It will also give sample output as array_coulmn & array_map.

  Now I've hope you know how to get particular key value from multidimensional array in PHP.

Releated Articles:

28 Apr 2016

Create a node programmatially in Drupal 7

      This blog describes about how to create a new node programmatically in Drupal 7. If you want to add a new node, you can done at node/add by default. In Drupal, you can also add a node programmatically. Let see the below code.

<?php
// create object
  $node = new stdClass();
  // set title for a node
  $node->title = t('Created node programmatically');
  // set node type
  $node->type = 'article';
  // set node language
  $node->language = LANGUAGE_NONE;
  // set value to node body
  $node->body[LANGUAGE_NONE][0]['value'] = t('This node has been created programmatically in Drupal 7');
  // set value to node body summary
  //$node->body[LANGUAGE_NONE][0]['summary'] = text_summary(t('This node has been created programmatically in Drupal 7'));
  // set node body format like plain_text, filtered_html, full_html
  $node->body[LANGUAGE_NONE][0]['format'] = 'filtered_html';
  node_object_prepare($node);
  // author for a node
  $node->uid = 1;
  // status of node  0 - unpublished, 1 - published
  $node->status = 1;
  // promoted to front page or not
  $node->promote = 0;
  // sitcky at top of tha page
  $node->sticky = 0;
  // comments 0 - hidden, 1 - closed, 2 - opened
  $node->comment = 1;

  // add term
  $node->field_tags[$node->language][]['tid'] = 1;

  // get the file path
  $file_path = drupal_get_path('module', 'phponwebsites') . '/Desert.jpg';
  // create file object
  $file = (object) array(
    'uid' => 1,
    'uri' => $file_path,
    'filemime' => file_get_mimetype($file_path),
    'status' => 1,
  );
  // Save the file to the public directory. You can specify a subdirectory, for example, 'public://images'
  $file = file_copy($file, 'public://');
  // assign the file object into image field
  $node->field_image[LANGUAGE_NONE][0] = (array)$file;
  // Prepare node for a submit
  $node = node_submit($node);
  //save the node
  node_save($node);


    After ran this code, you can see newly created node at admin/content. When you view that node, it looks like below image:

Create a new node programmatially in Drupal 7 at Phponwebsites


     Now I’ve hope you know how to create a new node programmatically in Drupal 7.

21 Apr 2016

Redirect users after login in Drupal 7

    This blog describes about how to redirect users after logged into a site in Drupal 7. By default, Drupal redirects users to user page after logged into a site.  Suppose you want to redirect users into any other pages as you want. Then you can done that in Drupal 7.
  You can redirect users after login in Drupal using the following two ways:
1. Redirect users after logged into a site using hook_user_login()
2. Redirect users after logged into a site using custom form submit

Redirect users after form submit in Drupal 7

Redirect users after logged into a site using hook_user_login:


     Drupal provides hook called hook_user_login to make changes while user login successfully. Let see the below code.

/**
 * Implement hook_user_login()
 */
function phponwebsites_user_login(&$form, &$form_state) {
 //add page here to where you want redirect users after login
  $form['redirect'] = '<front>';
}

    Now you can check whether you redirect to front page or not after login. Now  Drupal will be redirect you to front page.

Redirect users after logged into a site using custom form submit:


   Drupal have alternate method to redirect users after login. Ie, You need to add custom form submit handler to a form using hook_form_alter(). Then add a page to redirect users in that custom form submit handler in Drupal 7. Let see the below code.


/**
 * Implement hook_form_alter().
 */
function phponwebsites_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == "user_login" || $form_id == "user_login_block") {
    $form['#submit'][] = 'phponwebsites_custom_login_submit';
  }
}  
function phponwebsites_custom_login_submit(&$form, &$form_state) {
  //page to be redirect
  $form['redirect'] = '<front>';
}


Now you will be redirect to front page after logged into a drupal site. Now I’ve hope you should know how to redirect users after logged into a site in Drupal 7.