Skip to content

Instantly share code, notes, and snippets.

@msankhala
Last active February 9, 2017 07:35
Show Gist options
  • Select an option

  • Save msankhala/384d68ef6abac25a1455babb17d1516e to your computer and use it in GitHub Desktop.

Select an option

Save msankhala/384d68ef6abac25a1455babb17d1516e to your computer and use it in GitHub Desktop.
use-entity-metadata-wrapper. Also check http://www.ryanwright.me/cookbook/drupal7/entity/wrapper
<?php
// use entity metadata wrappers as they are SMART
$wrapper = entity_metadata_wrapper('node', $node);
dsm($wrapper->getPropertyInfo());
// Retrieving an integer value of a field
// Old approach
$showid = $node->field_seriesid[LANGUAGE_NONE][0]['value'];
// Entity metadata wrapper approach
$showid = $wrapper->field_seriesid->raw();
// Retrieving taxonomy term name in a single-select taxonomy
// Old approach
// Extract Te Reo level
$level = $show->field_te_reo_level;
$tid = $level[LANGUAGE_NONE][0]['tid'];
$term = taxonomy_term_load($tid);
$variables['level'] = $term->name;
// Entity metadata wrapper approach
$variables['level'] = $wrapper->field_te_reo_level->value()->name;
// Retrieving taxonomy term names in a multi-select taxonomy
// Old approach
// add the items from the 'show_features' taxonomy
$show_features = array();
$features = $series->field_features;
if (is_array($features[LANGUAGE_NONE])) {
foreach($features[LANGUAGE_NONE] as $tids) {
$tid = $tids['tid'];
$term = taxonomy_term_load($tid);
$featureList[] = $term->name;
}
}
// Entity metadata wrapper approach
// add the items from the 'show_features' taxonomy
$show_features = array();
foreach ($show_wrapper->field_features->value() as $index => $feature) {
$show_features[] = $feature->name;
}
// Language specific fields
// Entity metadata wrappers have native support for accessing the value of language specific fields (e.g. when using field translations),
// Entity metadata wrapper approach
global $language;
// get the localised title and body (hence the language param)
$title = $wrapper->language($language->language)->title_field->raw();
$body = $wrapper->language($language->language)->body->value->value(array('sanitize' => TRUE));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment