diff --git a/twitter_pull.field.inc b/twitter_pull.field.inc new file mode 100644 index 0000000..ac3fd14 --- /dev/null +++ b/twitter_pull.field.inc @@ -0,0 +1,186 @@ + array( + 'label' => 'Twitter Feed', + 'description' => 'Embed a twitter feed', + 'settings' => array(), + 'instance_settings' => array(), + 'default_widget' => 'twitter_pull_field_feed', + 'default_formatter' => 'twitter_pull_field', + ) + ); +} + +/** + * Implements hook_field_widget_info(). + */ +function twitter_pull_field_widget_info() { + return array( + 'twitter_pull_field_feed' => array( + 'label' => 'Feed', + 'description' => 'Provides a twitter feed field', + 'field types' => array('twitter_pull_field'), + 'settings' => array(), + 'behaviors' => array( + 'multiple values' => FIELD_BEHAVIOR_DEFAULT, + 'default value' => FIELD_BEHAVIOR_DEFAULT + ), + ), + ); +} + +/** + * Implements hook_field_widget_form(). + */ +function twitter_pull_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { + // Don't need to check the type right now because we're only defining one + $element = array( + '#type' => 'fieldset', + '#tree' => TRUE, + '#required' => $element['#required'], + '#description' => $element['#description'], + ); + + $element['key'] = array( + '#type' => 'textfield', + '#title' => t('Twitter key'), + '#default_value' => isset($items[$delta]['key']) ? $items[$delta]['key'] : '', + '#description' => t('Twitter key, which can be a username (prepended with @), hashtag (prepended with #), or a search term.'), + '#tree' => TRUE, + ); + + $element['title'] = array( + '#type' => 'textfield', + '#title' => t('Title'), + '#default_value' => isset($items[$delta]['title']) ? $items[$delta]['title'] : 'Tweets', + '#description' => t('Title of the feed.'), + '#tree' => TRUE, + ); + + $element['num'] = array( + '#type' => 'textfield', + '#title' => t('Number of tweets'), + '#default_value' => isset($items[$delta]['num']) ? $items[$delta]['num'] : '10', + '#description' => t('Number of tweets to retrieve from Twitter. Can\'t be more than 200.'), + '#tree' => TRUE, + ); + + return $element; +} + +/** + * Validates twitter feed. + */ +function twitter_pull_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) { + foreach ($items as $delta => $item) { + if (!is_numeric($item['num'])) { + $errors[$field['field_name']][$langcode][$delta][] = array( + 'error' => 'integer_check', + 'message' => t('%name: Number of tweets must be an integer.', array('%name' => $instance['label'])), + ); + } + + if ($item['num'] > 200) { + $errors[$field['field_name']][$langcode][$delta][] = array( + 'error' => 'num_limit', + 'message' => t('%name: Number of tweets cannot be larger than 200.', array('%name' => $instance['label'])), + ); + } + } +} + +/** + * Implements of hook_field_widget_error(). + */ +function twitter_pull_field_widget_error($element, $error, $form, &$form_state) { + switch ($error['error']) { + case 'integer_check': + case 'num_limit': + form_error($element['num'], $error['message']); + break; + } +} + +/** + * Implements of hook_field_is_empty(). + */ +function twitter_pull_field_is_empty($item, $field){ + return empty($item) || empty($item['key']) || $item['key'] === ''; +} + +/** + * Implements hook_field_formatter_info(). + */ +function twitter_pull_field_formatter_info() { + return array( + 'twitter_pull_field' => array( + 'label' => t('Twitter Feed'), + 'field types' => array('twitter_pull_field'), + 'settings' => array('key' => NULL, 'title' => 'Tweets', 'num' => 10, 'theme' => 'twitter_pull_listing'), + ), + ); +} + +/** + * Implements hook_field_formatter_settings_form(). + */ +function twitter_pull_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) { + $display = $instance['display'][$view_mode]; + $settings = $display['settings']; + $element = array(); + + if ($display['type'] == 'twitter_pull_field') { + $element['theme'] = array( + '#title' => t('Theme'), + '#type' => 'textfield', + '#default_value' => 'twitter_pull_listing', + ); + } + + return $element; +} + +/** + * Implements hook_field_formatter_settings_summary(). + */ +function twitter_pull_field_formatter_settings_summary($field, $instance, $view_mode) { + $display = $instance['display'][$view_mode]; + $settings = $display['settings']; + + if ($display['type'] == 'twitter_pull_field'){ + if (isset($settings['theme'])) { + $summary[] = t('Theme: @theme', array('@theme' => $settings['theme'])); + } + } + + return implode('
', $summary); +} + +/** + * Implements hook_field_formatter_view(). + */ +function twitter_pull_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display){ + $element = array(); + + foreach ($items as $delta => $item) { + dsm($item); + //Render the field + if ($display['type'] == 'twitter_pull_field') { + $element[$delta] = array( + '#markup' => twitter_pull_render($item['key'], $item['title'], $item['num'], $display['settings']['theme']) + ); + } + } + + return $element; +} \ No newline at end of file diff --git a/twitter_pull.install b/twitter_pull.install index 9a83e60..2092175 100644 --- a/twitter_pull.install +++ b/twitter_pull.install @@ -15,6 +15,38 @@ function twitter_pull_schema() { } /** + * Implements hook_field_schema(). + */ +function twitter_pull_field_schema($field){ + switch($field['type']){ + case 'twitter_pull_field' : + $columns = array( + 'key' => array( + 'type' => 'varchar', + 'length' => 512, + 'default' => '' + ), + 'title' => array( + 'type' => 'varchar', + 'length' => 512, + 'default' => '' + ), + 'num' => array( + 'type' => 'int', + 'default' => 10, + ), + ); + $indexes = array(); + break; + } + + return array( + 'columns' => $columns, + 'indexes' => $indexes + ); +} + +/** * Implements hook_requirements(). * * Report on whether we're using the authenticated api or not. @@ -26,7 +58,7 @@ function twitter_pull_requirements($phase) { return array( 'twittter_pull_auth' => array( 'title' => t('Twitter Pull Authentication'), - 'description' => t('Twitter Pull requires the !module to use the new Twitter API. The twitter API you are currently using will cease to function in the near future. See !link.', + 'description' => t('Twitter Pull requires the !module to use the new Twitter API. The twitter API you are currently using will cease to function in the near future. See !link.', array( '!link' => l('new twitter api information', 'https://dev.twitter.com/blog/changes-coming-to-twitter-api'), '!module' => l('Twitter module', 'http://drupal.org/project/twitter'), @@ -39,7 +71,7 @@ function twitter_pull_requirements($phase) { } // Load the twitter module. module_load_include('inc', 'twitter'); - + $twitter = twitter_connect(); if (!$twitter) { return array( diff --git a/twitter_pull.module b/twitter_pull.module index 48832ec..d7d2ff3 100644 --- a/twitter_pull.module +++ b/twitter_pull.module @@ -10,6 +10,9 @@ define ('TWITTER_PULL_CACHE_LENGTH', 20); //-- cache for 20 minutes define ('TWITTER_PULL_EMPTY_MESSAGE', 'No Tweets'); define ('TWITTER_PULL_CACHE_TABLE', 'cache_pulled_tweets'); +// Load all Field module hooks. +module_load_include('inc', 'twitter_pull', 'twitter_pull.field'); + function twitter_pull_num_items() { return variable_get('twitter_pull_num_items', TWITTER_PULL_NUM_ITEMS); }