Skip to content

Instantly share code, notes, and snippets.

@neotohin
Created December 2, 2015 07:15
Show Gist options
  • Save neotohin/1dc68852c2de438d03ff to your computer and use it in GitHub Desktop.
Save neotohin/1dc68852c2de438d03ff to your computer and use it in GitHub Desktop.
Drupal MetaData Wrapper Helper
<?php
/**
* A simple closure that helps working with node fields
*
* $metadata_wrapper = entity_metadata_wrapper('node', $nid);
* $wrapper = metaDataWrapperHelper( $metadata_wrapper );
*
* $field_name = $wrapper('field_name', 'text');
*/
function metaDataWrapperHelper($wrapper){
return function( $field, $type = null ) use($wrapper){
switch ($type) {
case 'text':
$val = $wrapper->{$field}->value();
return $val;
break;
case 'textarea':
$val = $wrapper->{$field}->value();
return $val;
break;
case 'textarea_formatted':
$val = $wrapper->{$field}->value();
return $val['safe_value'];
break;
case 'term_ids':
$val = $wrapper->{$field}->value();
if( is_array($val) ){
$arr = array();
foreach( $val as $obj){
$arr[] = $obj->tid ;
}
return $arr;
}else if( is_object($val ) ) {
return array( $val->tid );
}
return array();
break;
case 'term':
$val = $wrapper->{$field}->value();
if( is_object( $val) )
return $val->name;
break;
default:
# code...
return $wrapper->{$field}->value();
break;
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment