View source
<?php
function tweet_feed_field_info() {
return array(
'tweet_feed_user_mention' => array(
'label' => t('Tweet Feed: User Mentions'),
'description' => t('Stores data on users mentioned in a tweet'),
'default_widget' => 'tweet_feed_textfield',
'default_formatter' => 'tweet_feed_default',
),
);
}
function tweet_feed_field_is_empty($item, $field) {
if ($field['type'] == 'tweet_feed_user_mention') {
if (empty($item['tweet_feed_mention_id'])) {
return TRUE;
}
return FALSE;
}
}
function tweet_feed_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
if ($field['type'] === 'tweet_feed_user_mention') {
foreach ($items as $delta => $item) {
if (!empty($item)) {
foreach ($item as $k => $v) {
if ($v === '') {
$item[$k] = NULL;
}
}
$items[$delta] = $item;
}
}
}
}
function tweet_feed_field_widget_info() {
$widget = array();
$widget['tweet_feed_textfield'] = array(
'label' => t('Text field'),
'description' => t('Widget for Tweet Feed file field'),
'field types' => array(
'tweet_feed_user_mention',
),
);
return $widget;
}
function tweet_feed_field_formatter_info() {
return array(
'tweet_feed_user_mentions' => array(
'label' => t('User Mentions'),
'field types' => array(
'tweet_feed_user_mention',
),
),
'emoji_formatter' => array(
'label' => t('Emoji Formatter'),
'field types' => array(
'text_with_summary',
'text',
'text_long',
),
),
);
}
function tweet_feed_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = $mentions = array();
$connection = Database::getConnection();
$utf8mb4_configurable = $connection
->utf8mb4IsConfigurable();
$utf8mb4_active = $connection
->utf8mb4IsActive();
$utf8mb4_supported = $connection
->utf8mb4IsSupported();
$utf8 = $utf8mb4_configurable && $utf8mb4_active && $utf8mb4_supported ? TRUE : FALSE;
switch ($display['type']) {
case 'tweet_feed_user_mentions':
foreach ($items as $delta => $item) {
$mentions[] = $item['tweet_feed_mention_name'] . ' (' . $item['tweet_feed_mention_screen_name'] . '): ' . $item['tweet_feed_mention_id'];
}
if (!empty($delta)) {
$mentions = implode('<br />', $mentions);
$element[$delta] = array(
'#markup' => $mentions,
);
}
break;
case 'emoji_formatter':
foreach ($items as $delta => $item) {
if ($utf8 === TRUE) {
$element[$delta] = array(
'#markup' => $item['value'],
);
}
else {
$element[$delta] = array(
'#markup' => utf8_decode($item['value']),
);
}
}
break;
}
return $element;
}
function tweet_feed_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
if ($field['type'] == 'tweet_feed_user_mention') {
$element['tweet_feed_mention_name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => isset($items[$delta]['tweet_feed_mention_name']) ? $items[$delta]['tweet_feed_mention_name'] : '',
'#size' => 60,
'#maxlength' => 255,
);
$element['tweet_feed_mention_screen_name'] = array(
'#type' => 'textfield',
'#title' => t('Screen Name'),
'#default_value' => isset($items[$delta]['tweet_feed_mention_screen_name']) ? $items[$delta]['tweet_feed_mention_screen_name'] : '',
'#size' => 60,
'#maxlength' => 255,
);
$element['tweet_feed_mention_id'] = array(
'#type' => 'textfield',
'#title' => t('ID'),
'#default_value' => isset($items[$delta]['tweet_feed_mention_id']) ? $items[$delta]['tweet_feed_mention_id'] : '',
'#size' => 60,
'#maxlength' => 255,
);
}
return $element;
}