On Github fmitchell / 30Drupal7API
Created by Fredric Mitchell / @fredricmitchell
http://brightplumbox.com/30Drupal7API/
Senior Developer at Phase2 Technology
Drupal, DRY / KISS, Strat. Comm., Graphic Novels
Javascript, Patience, Spanish
Quick and easy ways to find API functions and Drupal projects.
http://bit.ly/18XPrjF
http://bit.ly/1dbB0eL
$text = t(“@name's blog”, array('@name' => $value))
// Note: @name vs. %name vs. !name
$link = l(t('Link text'), 'node/34', array(
'attributes' => array(
'class' => array('about-link'),
)
));
// Note: automatic alias replacement via url()
$render = array(
'first_para' => array(
'#type' => 'markup',
'#markup' => 'A first paragraph',
),
'second_para' => array(
array('first item', 'second item', 'third item'),
'#theme' => 'item_list',
),
);
return drupal_render($render);
$nodes = node_load_multiple($nids = array())
// Note: calls entity_load(); statically cached
$node = node_view($node, $view_mode);
$nodes = node_view_multiple($nodes);
node_delete_multiple($nids = array());
$node = new StdClass();
// Set type first.
$node->type = 'foobar';
node_object_prepare($node);
// Note: Current user will be set as uid.
node_save($node);
// Note: nothing is returned
function mymodule_menu() {
$items['abc/def'] = array(
'page callback' => 'mymodule_abc_view',
);
return $items;
}
function mymodule_abc_view($ghi = 0, $jkl = '') {
// ...
}
$item = menu_get_item($path);
// Note: returns array defined in hook_menu()
$node = menu_get_object();
$type = $node->type;
// Note: Use instead of arg()
$tree = menu_tree_page_data('main-menu');
// Build a HTML menu
$tree = menu_tree_page_data('main-menu');
$menu = menu_tree_output($tree, 1);
return drupal_render($menu);
// Get a taxonomy object.
$v = taxonomy_vocabulary_machine_name_load('vocab');
// Get a taxonomy object.
$v = taxonomy_vocabulary_machine_name_load('vocab');
// Build an array of taxonomy terms.
$terms = taxonomy_get_tree($v->vid);
foreach ($terms as $term) {
$options[$term->tid] = $term->name;
}
// Get info a field name.
field_info_field('field_name');
// Get info on field name.
field_info_field('field_name');
// Get info on instance of field name from a bundle.
field_info_instance('node', 'field_name', 'article');
// Note: Requires proper field / instance data array which you can get from a features export.
$field_bases = array();
// Exported field_base: 'field_sc_html'
$field_bases['field_sc_html'] = array(
'active' => 1,
'cardinality' => 1,
'deleted' => 0,
'entity_types' => array(),
'field_name' => 'field_sc_html',
'foreign keys' => array(
'format' => array(
'columns' => array(
'format' => 'format',
),
'table' => 'filter_format',
),
),
'indexes' => array(
'format' => array(
0 => 'format',
),
),
'locked' => 0,
'module' => 'text',
'settings' => array(),
'translatable' => 0,
'type' => 'text_long',
);
// Note: Requires proper field / instance data array which you can get from a features export.
$field_instances = array();
// Exported field_instance: 'bean-field_sc_html'
$field_instances['bean-field_sc_html'] = array(
'bundle' => 'oms_sidebar_content',
'default_value' => NULL,
'deleted' => 0,
'description' => '',
'display' => array(
'default' => array(
'label' => 'above',
'settings' => array(),
'type' => 'hidden',
'weight' => 0,
),
),
'entity_type' => 'bean',
'field_name' => 'field_sc_html',
'label' => 'HTML',
'required' => 0,
'settings' => array(
'text_processing' => 1,
'user_register_form' => FALSE,
),
'widget' => array(
'active' => 1,
'module' => 'text',
'settings' => array(
'rows' => 5,
),
'type' => 'text_textarea',
'weight' => 2,
),
);
function mymodule_install_fields($fields, $instance_fields, $type = 'node', $bundle) {
foreach ($fields as $field) {
// Create the field if it doesn't exist.
if (!field_info_field($field['field_name'])) {
field_create_field($field);
}
}
unset($field);
foreach ($instance_fields as $field) {
if (!field_info_instance($type, $field['field_name'], $bundle)) {
field_create_instance($field);
}
}
}
// Update field details.
$field = field_info_field('field_name');
if (isset($my_condition)) {
field_update_field($field);
}
// Update field instance.
$instance = field_info_instance('node', $field_name, $bundle);
if ($instance) {
field_update_instance($instance);
}
// Note: Can use in hook_update() to avoid UI config conflicts.
$data = array(
'key1' => 'value1',
'key2' => 'value2',
);
// Call all modules implementing hook_my_data_alter
drupal_alter('my_data', $data);
// Note: Use sparingly. hook_theme() is preferred for overriding styles.
function mymodule_form_alter(&$form, &$form_state) {
$form['new_checkbox'] = array(
'#type' => 'checkbox',
'#title' => t ('Subscribe'),
);
);
function mymodule_form_node_form_alter() {
...
);
// URL: http://mysite.com?text=foo&date=2013-09-14
$param = drupal_get_query_parameters();
// Values
// $param['text'] = foo
// $param['date'] = 2013-09-14
// Path to a file
$path = drupal_get_path('theme', 'mytheme') . '/mycustom.js';
// Include CSS or JS
drupal_add_js($path), array(
'type' => 'file',
'scope' => 'footer',
));
// Note: Also consider libraries_get_path()
// Load node.admin.inc from the node module.
module_load_include('inc', 'node', 'node.admin');
// Load content_types.inc from the node module.
module_load_include('inc', 'node', 'content_types');
// Query articles
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'article')
->propertyCondition('status', 1);
$result = $query->execute();
$nids = array_keys($result['node']);
$nodes = node_load_multiple($nids);
// Note: EFQ presentation at http://bit.ly/top10efq
Slides: http://brightplumbox.com/30Drupal7API/
Slides Repo: https://github.com/fmitchell/30Drupal7API
Feedback: joind.in/talk/view/10311