Thursday, January 28, 2016

Drupal 7: Set Default Image to Content with Update Hook


Here is the update hook to add default image to article content:

  1. /**
  2. * Update default image on field base.
  3. */
  4. function mymodule_update_7300() {
  5. $field_name = 'field_fb_article_image';
  6. $filename = 'images.png';
  7. $source = drupal_get_path('module', 'mymodule') . '/includes/images' . '/' . $filename;
  8. // Load field info.
  9. $field = field_info_field($field_name);
  10. // Create new file object and get new fid.
  11. if (file_exists($source)) {
  12. $file = new stdClass;
  13. $file->filename = $filename;
  14. $file->timestamp = REQUEST_TIME;
  15. $file->uri = $source;
  16. $file->filemime = file_get_mimetype($source);
  17. $file->uid = 1;
  18. $file->status = 1;
  19. $file = file_copy($file, 'public://files', FILE_EXISTS_REPLACE);
  20. $fid = $file->fid;
  21. // Replace old fid with new fid.
  22. $field['settings']['default_image'] = (string) $fid;
  23. // Update field.
  24. field_update_field($field);
  25. }
  26. }
  27.  

Wednesday, January 27, 2016

Drupal 7: How to create update hook



Step 1: Go to the following path and create update hook to perform the action.

sites/all/modules/custom/mymodule/mymodule.install
  1. /**
  2. * Disabled PSL Newsletter module
  3. * Implements hook_update_N()
  4. */
  5. function mymodule_update_7300(&$sandbox) {
  6. module_disable(array('newsletter'));
  7. return 'Newsletter has been disabled';
  8. }
  9.  

Step 2: Go to project root path and run drush updb -y

Drupal 7: Calling Session Variable with Javascript


Step 1: Prepare hook menu to call getSession function. Return the $result variable after this line $result = $_SESSION['_custom_variables'];

sites/all/modules/custom/mymodule/mymodule.module
  1. /** Comment **/
  2. /**
  3. * Implements hook_menu().
  4. */
  5. function mymodule_menu() {
  6. // track url
  7. $items['getSession'] = array(
  8. 'page callback' => 'mymodule_getSession_ajax',
  9. 'access callback' => TRUE
  10. );
  11. return $items;
  12. }
  13. /**
  14. * Called by ajax request to getSession
  15. */
  16. function mymodule_getSession_ajax() {
  17. if(isset($_POST['action'])) {
  18. $action = $_POST['action'];
  19. unset($_POST['action']);
  20. }
  21. else {
  22. $action = 'default';
  23. }
  24. mymodule_getSession('ajax', $action, $_POST);
  25. }
  26. /**
  27. *
  28. * @param string $type ajax|server This informs the function if it should use the current request or REFERER as the url
  29. * @param string $action action to send
  30. * @param array $vars Associative array of variables to send as custom vars
  31. */
  32. function mymodule_getSession($type, $action, $vars=array()){
  33. $url = $_SERVER['HTTP_REFERER'];
  34. $params = parse_url($url);
  35. $result = array();
  36. $customVariable = ['MemberID', 'EmailID', 'SpecialtyID'];
  37. if(isset($params['query'])){
  38. parse_str($params['query'], $query);
  39. foreach ($query as $key => $value) {
  40. if(in_array($key, $customVariable))
  41. {
  42. $_SESSION['_custom_variables'][$key] = $value;
  43. }
  44. }
  45. }
  46. $result = $_SESSION['_custom_variables'];
  47. print drupal_json_output($result);
  48. exit();
  49. }


Step 2: Call hook menu with ajax, return the result after success call.

sites/all/themes/mytheme/js/mytheme.js
  1. /** Comment **/
  2. (function ($) {
  3. jQuery(document).ready(function() {
  4. var mysession = getSession();
  5. console.log(mysession);
  6. /*
  7. * Get Session Function
  8. * @action is the name of the page
  9. * @actionType is either 'page_view' or 'link'
  10. */
  11. function getSession() {
  12. // add action and action type
  13. var data = {action:'custom_variables'};
  14. var basePath = '';
  15. var result;
  16. if ( (Drupal.settings.basePath != undefined) && (Drupal.settings.basePath != '/') ) {
  17. basePath = Drupal.settings.basePath;
  18. }
  19. basePath = basePath + '/getSession';
  20. basePath = basePath.replace('//', '/');
  21. jQuery.ajax({
  22. url: basePath,
  23. type:'POST',
  24. dataType:'json',
  25. data:data,
  26. async:(jQuery.browser.safari || jQuery.browser.mozilla) ? false : true,
  27. success: function(data) {
  28. // for this current implementation, there is no need for action upon success
  29. result = data;
  30. },
  31. error: function(xhr, status, error) {
  32. // error
  33. }
  34. });
  35. return result;
  36. }
  37. })
  38. }
  39. )(jQuery);