Last active
February 11, 2016 18:12
-
-
Save woombo/b1fe378e3c5f17e46c49 to your computer and use it in GitHub Desktop.
hook_inline_entity_form_table_fields_alter()
This file contains hidden or 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_inline_entity_form_table_fields_alter(). | |
*/ | |
function MODULE_inline_entity_form_table_fields_alter(&$fields, $context) { | |
if ($context['parent_bundle'] === 'MODULE' && | |
$context['entity_type'] === 'MODULE_text_asset') { | |
// Remove Title. | |
unset($fields['id']); | |
// Change label from "Title" to "Question". | |
$fields['title']['label'] = t('Question'); | |
// Answer. | |
$fields['field_qna_answer'] = array( | |
'type' => 'callback', | |
'render_callback' => '_MODULE_render_textarea', | |
'label' => t('Answer'), | |
'weight' => 3 | |
); | |
// Link. | |
$fields['field_slide_link'] = array( | |
'type' => 'field', | |
'label' => t('Link'), | |
'weight' => 3 | |
); | |
// Add new fields. | |
$fields['field_slide_image'] = array( | |
'type' => 'callback', | |
'render_callback' => '_MODULE_render_thumbnail', | |
'label' => t('Image'), | |
'weight' => 2 | |
); | |
} | |
} | |
/** | |
* Custom callback to truncate text size on a pre-defined size. | |
* | |
* @param string $entity_type | |
* Entity type being processed. | |
* @param object $entity | |
* Entity object. | |
* | |
* @return string | |
* Truncated text. | |
*/ | |
function _MODULE_render_textarea($entity_type, $entity) { | |
if (isset($entity->field_qna_answer[LANGUAGE_NONE][0]['safe_value'])) { | |
$string = $entity->field_qna_answer[LANGUAGE_NONE][0]['safe_value']; | |
return strlen($string) > 200 ? text_summary($string, NULL, 200) . ' (...)' : $string; | |
} | |
} | |
function _MODULE_render_thumbnail($entity_type, $entity) { | |
if (isset($entity->field_slide_image[LANGUAGE_NONE][0]['uri'])) { | |
return '<img src="' . image_style_url('thumbnail', $entity->field_slide_image[LANGUAGE_NONE][0]['uri']) .'" />'; | |
} | |
return 'N/A'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment