Created
March 1, 2015 08:57
-
-
Save HelenaEksler/df2ab07f15003f093d49 to your computer and use it in GitHub Desktop.
Custom field formatter view for the body field.
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
/** | |
* Implements hook_field_formatter_info(). | |
*/ | |
function c4m_message_field_formatter_info() { | |
return array( | |
'summary_with_image' => array( | |
'label' => t('Summary or trimmed with image'), | |
'field types' => array('text_with_summary'), | |
) | |
); | |
} | |
/** | |
* Implements hook_field_formatter_view(). | |
*/ | |
function c4m_message_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { | |
$element = array(); | |
switch ($display['type']) { | |
case 'summary_with_image': | |
foreach ($items as $delta => $item) { | |
$wrapper = entity_metadata_wrapper($entity_type, $entity); | |
$body = $wrapper->c4m_body->value->value(); | |
$image = ''; | |
if (strpos($body, '<img') && strpos($body, '<img') < 600) { | |
$pattern = '/<img\s+[^>]*?src="([^"]+)"[^>]+>/i'; | |
preg_match($pattern, $body, $images_uri); | |
$image_uri = $images_uri[1]; | |
$body = preg_replace('/<img[^>]+\>/i', '', $body); | |
$image = theme('html_tag', array( | |
'element' => array( | |
'#tag' => 'img', | |
'#attributes' => array( | |
'class' => 'img-responsive', | |
'typeof' => 'foaf:Image', | |
'src' => $image_uri, | |
), | |
) | |
)); | |
} | |
$element[$delta] = array( | |
'#theme' => 'c4m_message_summary_with_image', | |
'#image' => $image, | |
'#description' => substr($body, 0, 600), | |
); | |
} | |
break; | |
} | |
return $element; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what is c4m_body ?
i received "EntityMetadataWrapperException: Unknown data property c4m_body."