Skip to content

Instantly share code, notes, and snippets.

@raphaellarrinaga
Last active July 1, 2020 07:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save raphaellarrinaga/3b8011676da3fcb52236c495b5c95565 to your computer and use it in GitHub Desktop.
Save raphaellarrinaga/3b8011676da3fcb52236c495b5c95565 to your computer and use it in GitHub Desktop.
[Drupal 7 php/config cheatsheet] #tags: drupal7, php, cheatsheet, config

Drupal 7 PHP cheatsheet

Get the NID of the current node

Assuming your code is running for a node page, the methods I see used most often in core/contrib modules are either using menu_get_object() or arg():

if ($node = menu_get_object()) {
  // Get the nid
  $nid = $node->nid;
}

or

if (arg(0) == 'node' && is_numeric(arg(1))) {
  // Get the nid
  $nid = arg(1);

  // Load the node if you need to
  $node = node_load($nid);
}

I personally prefer the first method (even though assignment in condition isn't considered a good idea by some people), but both are perfectly valid.

@see https://drupal.stackexchange.com/questions/32309/how-to-programmatically-get-the-nid-of-the-current-node

Node URL alias from nid

$alias = drupal_get_path_alias('node/37');

Create fields with drush

lando drush field-create my_content_type my_field_name,image,media_generic;
lando drush field-create my_content_type my_field_name,link_field,link_field;
lando drush field-create my_content_type my_field_name,entityreference,entityreference_autocomplete;
lando drush field-create my_content_type my_field_name,text,text_textfield;
lando drush field-create my_content_type my_field_name,text_long,text_textarea;
lando drush field-create my_content_type my_field_name,taxonomy_term_reference,options_select

Rerun a hook_update_N()

lando drush eval "module_load_install('my_module_containing_hook'); my_module_containing_hook_update_7001();"

Fix missing uninstalled / undisabled module error message

lando drush sql-query "DELETE from system where type = 'module' AND name = 'my_module_name';"

Delete field with drush

lando drush field-delete field_name -y

Test for node type outside node_preprocess context

function hook_preprocess_html(&$variables) {
  if (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) {
    $node = node_load(arg(1));
    $node_type = $node->type;
  }
}

Get original image path

From hook_preprocess_node()

$catalog_image_original = file_create_url($variables['field_catalog_image'][0]['uri']);

Render fields.

@see https://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way

Pass a variable to javascript.

drupal_add_js(drupal_get_path('theme', 'theme_name') . '/js/filename.js');
drupal_add_js(array(
  'catalogFull' => array(
    'imageUrl' => $my_variable,
  ),
), 'setting');

Get it in the js:

(function($) {
  Drupal.behaviors.catalogFull = {
    attach: function(context, settings) {
      const imageUrl = Drupal.settings['catalogFull']['imageUrl'];
    }
  }
})(jQuery);

Export nodes in a feature via node export + feature submodule

Node export has a feature integration but only the first 250 nodes are available. A trick mentioned here propose to add this function temporarily to limit nodes to sticky ones.

function orange_webform_extra_query_alter($query) {
  if ($query->hasTag('node_export_features')) {
    $query->condition('sticky', 1);
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment