Created
December 2, 2015 07:15
-
-
Save neotohin/1dc68852c2de438d03ff to your computer and use it in GitHub Desktop.
Drupal MetaData Wrapper Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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