Drupal How to add JavaScript specific for a view?

http://drupal.stackexchange.com/a/29217/26771:

You could use the preprocess_views_view() hook to do this:

function THEME_preprocess_views_view(&$vars) {
  $view = &$vars['view'];
  // Make sure it's the correct view
  if ($view->name == 'your-view-name') {
    // add needed javascript
    drupal_add_js(drupal_get_path('theme', 'your-theme') . '/your-js.js');
    // add needed stylesheet
    drupal_add_css(drupal_get_path('theme', 'your-theme') .'/your-css.css');
  }
}

Note that you could also check the specific display of the view using:

if ($view->name == 'your-view-name' && $view->current_display == 'your-display-id') {
  // include javascript & css here
}

You should be able to check for a custom css class like this:

if ($view->name == 'your-view-name' && $view->display[$view->current_display]->display_options['css_class'] == 'your-css-class') {
   // include javascript & css here
}

0. http://drupal.stackexchange.com/questions/29210/how-do-i-add-javascript-specific-for-a-view
1. https://api.drupal.org/api/views/theme!theme.inc/function/template_preprocess_views_view/7

Drupal 7 create tabs with hook_menu() for custom module

Define a ‘root’ path which is a normal menu item, and then add another item for each of the tabs. The first of these will inherit the properties of the root item, and the second will define it’s own callbacks. Combined with the MENU_LOCAL_TASK and MENU_DEFAULT_LOCAL_TASK type constants

/**
 * Implements hook_menu().
 */
function timetools_menu() {
  $items = array();

  // define parent item
  $items['timetools'] = array(
    'title' => 'Timetools',
    'page callback' => 'timetools_timestamp_page',
    'access arguments' => array('access content'),
    'file' => 'timetools.timestamp.inc',
    'weight' => 1,
  );
  
  // define first tab
  $items['timetools/timestamp'] = array(
    'title' => 'Unix Timestamp Converter',
    'weight' => -1,
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  
  // define second tab
  $items['timetools/datecalc'] = array(
    'title' => 'Mini Date Calculator',
    'page callback' => 'timetools_datecalc_page',
    'access arguments' => array('access content'),
    'file' => 'timetools.datecalc.inc',
    'weight' => 2,
    'type' => MENU_LOCAL_TASK,
  );

  return $items;
}

Resources:
0. http://drupal.stackexchange.com/questions/37262/create-tabs-with-hook-menu-for-custom-module

1. https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_menu/7

Create a user account in Drupal 7 programmatically

//This will generate a random password, you could set your own here
$password = user_password(8);

$new_user = array(
  'name' => $name,
  'pass' => $password, // note: do not md5 the password
  'mail' => $email,
  'status' => 1,
  'init' => $email,
  'roles' => array(
    DRUPAL_AUTHENTICATED_RID => 'authenticated user',
    3 => 'custom role',
  ),
);

// The first parameter is sent blank so a new user is created.
user_save('', $new_user);

To programmatically create a user with roles and custom field values

$new_user = array(
  'name' => $name,
  'pass' => $password, // note: do not md5 the password
  'mail' => $email,
  'status' => 1,
  'init' => $email,
  'roles' => array(
    DRUPAL_AUTHENTICATED_RID => 'authenticated user',
    3 => 'custom role',
  ),
  'field_last_name' => array(
     'und' => array(
        0 => array(
          'value' => 'Parsons',
        ),
      ),
   ),
);

// The first parameter is sent blank so a new user is created.
user_save('', $new_user);

Using entity_metatda_wrapper

$wrapper = entity_metadata_wrapper('user', $account);
$wrapper->field_firstname->set('John');
$wrapper->field_lastname->set('Doe');
$wrapper->save;

Drupal 7 функції для роботи з базою даних


//////////////////////
//=db_query

$uid = 1;
$result = db_query(
  'SELECT n.nid, n.title, n.created FROM {node} n WHERE n.uid = :uid',
   array(':uid' => $uid)
);
// Result is returned as a iterable object that returns a stdClass object on each iteration
foreach ($result as $record) {
  // Perform operations on $record->title, etc. here.
  // in this example the available data would be mapped to object properties:
  // $record->nid, $record->title, $record->created
}

$uid = 1;
$result = db_query(
  'SELECT n.nid, n.title, n.created FROM {node} n WHERE n.uid = :uid',
   array(':uid' => $uid)
);

// Fetch next row as a stdClass object.
$record = $result->fetchObject();

// Fetch next row as an associative array.
$record = $result->fetchAssoc();

// Fetch data from specific column from next row
// Defaults to first column if not specified as argument
$data = $result->fetchColumn(1); // Grabs the title from the next row


// Retrieve all records into an indexed array of stdClass objects.
$result->fetchAll();

// Retrieve all records as stdObjects into an associative array 
// keyed by the field in the result specified. 
// (in this example, the title of the node)
$result->fetchAllAssoc('title');

// Retrieve a 2-column result set as an associative array of field 1 => field 2.
$result->fetchAllKeyed();

// Also good to note that you can specify which two fields to use
// by specifying the column numbers for each field
$result->fetchAllKeyed(0, 2); // would be nid => created
$result->fetchAllKeyed(1, 0); // would be title => nid

// Retrieve a 1-column result set as one single array.
$result->fetchCol();

// Column number can be specified otherwise defaults to first column
$result->fetchCol($db_column_number);

// Count the number of rows
$result->rowCount();

// get one row from db
$result->fetch();

// You can use fetchField() to get a single value
$result->fetchField();


// get one row from db
$result = db_query(
  'SELECT n.nid, n.title, n.created FROM {node} n WHERE n.uid = :uid LIMIT 1',
   array(':uid' => $uid)
)
->fetch();

// You can use fetchField() to get a single value
$result = db_query(
  'SELECT n.nid, n.title, n.created FROM {node} n WHERE n.uid = :uid LIMIT 1',
   array(':uid' => $uid)
)
->fetchField();

// 
db_query("TRUNCATE TABLE {api_log}");


//
db_query("SELECT nid FROM node ")->range(0, 3);

//
$sql = "SELECT name, quantity FROM goods WHERE vid = :vid";
$result = db_query($sql, array(':vid' => $vid));
if ($result) {
  while ($row = $result->fetchAssoc()) {
    // Do something with:
    //    $row['name']
    //    $row['quantity']
  }
}

//////////////////////////
//=db_select

$result = db_select('node', 'n')
    ->fields('n')
    ->condition('nid', $node->nid,'=')
    ->condition('status', 0,'>')
    ->condition('uid', array(1,5,7),'IN')
    ->execute()
    ->fetchAssoc();

//------------------------------------------------------------------
// Create an object of type SelectQuery
$query = db_select('users', 'u');

// Add extra detail to this query object: a condition, fields and a range
$query->condition('u.uid', 0, '<>');
$query->fields('u', array('uid', 'name', 'status', 'created', 'access'));
$query->range(0, 50);

$result = $query->execute();
foreach ($result as $record) {
  // Do something with each $record
}


//-------------------------------------------------------------------
$query = db_select('webform_submitted_data','wb');

$query->join('webform_submissions','w','wb.sid = w.sid');

$query->condition('w.uid','1')
      ->fields('w',array('sid','uid','nid'))
      ->fields('wb',array('data'));

$result = $query->execute();
foreach($result as $record){
  print  $record->sid.$record->data;
}

//--------------------------------------------------
$query = db_select('node', 'n');

$query->join('users', 'u', 'n.uid = u.uid'); //JOIN node with users
$query->groupBy('u.uid');//GROUP BY user ID

$query->fields('n',array('title','created'))//SELECT the fields from node
    ->fields('u',array('name'))//SELECT the fields from user
    ->orderBy('created', 'DESC')//ORDER BY created
    ->range(0,2);//LIMIT to 2 records
    

$result = $query->execute();
while($record = $result->fetchAssoc()) {
  print_r($record);
}

//////////////////////////
//=db_insert

// insert one record
$last_insert_id = db_insert('uc_cart_products')
      ->fields(array(
        'cart_id' => $cid,
        'nid' => $nid,
        'qty' => $qty,
        'changed' => REQUEST_TIME,
        'data' => serialize($data),
      ))
      ->execute();

// insert multiple records at a time
$values = array(
  array(
    'title' => 'Example',
    'uid' => 1,
    'created' => REQUEST_TIME,
  ),
  array(
    'title' => 'Example 2',
    'uid' => 1,
    'created' => REQUEST_TIME,
  ),
  array(
    'title' => 'Example 3',
    'uid' => 2,
    'created' => REQUEST_TIME,
  ),
);

$query = db_insert('node')->fields(array('title', 'uid', 'created'));
foreach ($values as $record) {
  $query->values($record);
}
$query->execute();

//////////////////////////
//=db_update

db_update('uc_cart_products')
  ->fields(
    array(
      'changed' => REQUEST_TIME,
      'data' => serialize($data),
    )
  )
  ->condition('cart_item_id', $cart_item_id, '=')
  ->execute();

    
//////////////////////////
//=db_delete
db_delete('node')
  ->condition('nid', $nid)
  ->execute();


////////////////////////////
//=db_truncate

db_truncate('node')->execute();


//////////////////////////////////
//=db_query_range
db_query_range("SELECT nid FROM node", 0, 3);

$result = db_query_range(
  'SELECT n.nid, n.title, n.created
  FROM {node} n WHERE n.uid = :uid
  ORDER BY n.created DESC', 0, 10, array(':uid' => $uid));

foreach ($result as $record) {
  // Perform operations on $record->title, etc. here.
}

Drupal 7 перевірка на відсутні модулі

function MYMODULE_init() {

    $startingtime = microtime(true); 
    $o = '<p>Checking for dead modules ...</p>';
    $result = db_select('system')
      ->fields('system', array('filename'))
      ->condition('status', '1', '=')
      ->execute();
      $n = 1;
      $m = 0;
    foreach ($result as $row) {
      $path = DRUPAL_ROOT.'/'.$row->filename;
      If (!file_exists($path)) { 
          $o .= "#$n $path<br>";
          $m++;
      }
      $n++;
    }
    $timedif =  round(microtime(true) - $startingtime,3);
    $o .= "Total of $n active modules registered in database. $m dead entries found.<br>";
    $o .= 'Query Time: '.$timedif.' seconds';

    drupal_set_message($o);

}

Resources
http://drupal.stackexchange.com/questions/78595/drupal-calling-is-dir-over-4000-times