You are here

Code

Creating and consuming a JSON file

paulbooker's picture
Submitted by paulbooker on Mon, 06/05/2017 - 17:46

Creating a JSON file:

/**
 * Implementation of hook_menu().
 */
function example_module_menu() {
  $items['careers/json'] = array(
    'access callback'   => true,
    'page callback'     => 'example_module_careers', // call function below
    'delivery callback' => 'drupal_json_output' // output as json
  );
  return $items;
}


function example_module_careers() {

  $query = db_select('node', 'n');
  $query->fields('n', array('nid'));
  $query->condition('n.type', 'career', '='); 
  
  $number_of_jobs = $query->countQuery()->execute()->fetchField();

  //print($number_of_jobs);

  $query = db_select('node', 'n');
  $query->leftjoin('field_data_field_quantity', 'fq', 'fq.entity_id = n.nid');
  $query->fields('n', array('nid'));
  $query->fields('fq', array('field_quantity_value'));
  $query->condition('n.type', 'career', '='); 

  $result = $query->execute();
  
  $number_of_positions = 0;
  foreach ($result as $record) {	
	  $number_of_positions += $record->field_quantity_value;
  }
  //print($number_of_positions);
 
  $results = array('number_of_jobs' => $number_of_jobs, 'number_of_positions' => $number_of_positions);
  //print_r($results); 
  //exit;
  return array('results'=>$results,);
  
}

Consuming a JSON file:

if ($json_url) {
  $request = drupal_http_request($json_url);
  $careers = drupal_json_decode($request->data);
  $number_of_jobs = $careers['results']['number_of_jobs'];
  $number_of_positions = $careers['results']['number_of_positions'];
} 

This could be placed inside template.php or inside a Views Global: PHP field.

Adapting an existing Drupal theme for mobile devices.

paulbooker's picture
Submitted by paulbooker on Fri, 05/05/2017 - 18:11
@media screen and (max-width: 680px) {
  #head,
  #page,
  #footer-region {
    width: 100%;
  }
  
  #content,
  #sidebar-primary {
    float: none;
    width: 100%;
  }
  
  #sidebar-primary .block {
    float: none;
    width: 100%;
  }
	
  .sidebar-first #content {
    width: 100%;	
  }	 
	
  #triptych-first, #triptych-second {
    padding: 0;
  }
	
  img {
    max-width: 300px;
    height: auto;
  }
  
  #logo  {
    bottom: auto !important;
    position: inherit !important;
    right: auto !important;
  }  	
}
/**
 * Implements hook_page_alter().
 */
function mytheme_page_alter($page) {
  $viewport = array(
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => array(
      'name' => 'viewport',
      'content' => 'width=device-width, initial-scale=1',
    ),
  );
  drupal_add_html_head($viewport, 'viewport');
}

Modifying the meta tag on all Drupal pages.

paulbooker's picture
Submitted by paulbooker on Mon, 10/17/2016 - 12:07
function mymodule_html_head_alter(&$head_elements) {
	  
  $head_elements['system_meta_content_type']['#attributes'] = array(
    'charset' => 'utf-8',
    'http-equiv' => "X-UA-Compatible"
  );	
}

Pages