diff --git a/twitter_post/twitter_post.field.inc b/twitter_post/twitter_post.field.inc index 2d39872..df1a793 100644 --- a/twitter_post/twitter_post.field.inc +++ b/twitter_post/twitter_post.field.inc @@ -83,7 +83,6 @@ function twitter_post_field_widget_form(&$form, &$form_state, $field, $instance, '#dialog' => TRUE, ), ), - ); return $element; @@ -122,7 +121,6 @@ function twitter_post_field_formatter_settings_summary($field, $instance, $view_ $display = $instance['display'][$view_mode]; $settings = $display['settings']; - if ($settings['type'] != 'none') { $summary = t('Default status: @status. Message: @message', array( '@status' => $settings['status'], @@ -170,45 +168,54 @@ function twitter_post_field_attach_form($entity_type, $entity, &$form, &$form_st $twitter_uid = key($options); $screen_name = current($options); foreach ($form_state['field'] as $field_name => $field) { - if (isset($field[$langcode]['field']['type']) && ($field[$langcode]['field']['type'] == 'twitter_post')) { - $element = array(); - if (count($options) > 1) { - $element['account'] = array( - '#type' => 'select', - '#title' => t('Twitter Account'), - '#options' => $options, - '#id' => 'twitter-account', - '#states' => array( - 'visible' => array( - 'input#twitter-toggle' => array( - 'checked' => TRUE, + // Loop over each interior field. Cannot assume that the entity's langcode + // is the same as the field's because of EntityTranslation and others. + foreach ($field as $field_langcode => $field_data) { + if (isset($field_data['field']['type']) && ($field_data['field']['type'] == 'twitter_post')) { + $element = array(); + if (count($options) > 1) { + $default = NULL; + if (isset($entity->{$field_name}[$field_langcode][0])) { + $default = $entity->{$field_name}[$field_langcode][0]; + } + $element['account'] = array( + '#type' => 'select', + '#title' => t('Twitter Account'), + '#options' => $options, + '#id' => 'twitter-account', + '#default_value' => $default, + '#states' => array( + 'visible' => array( + 'input#twitter-toggle' => array( + 'checked' => TRUE, + ), ), ), - ), - ); - } - else { - $element['account'] = array( - '#type' => 'value', - '#value' => $twitter_uid, - '#id' => 'twitter-account', - ); - $element['account_name'] = array( - '#type' => 'item', - '#title' => t('Twitter account'), - '#markup' => _twitter_user_profile($screen_name), - '#states' => array( - 'visible' => array( - 'input#twitter-toggle' => array( - 'checked' => TRUE, + ); + } + else { + $element['account'] = array( + '#type' => 'value', + '#value' => $twitter_uid, + '#id' => 'twitter-account', + ); + $element['account_name'] = array( + '#type' => 'item', + '#title' => t('Twitter account'), + '#markup' => _twitter_user_profile($screen_name), + '#states' => array( + 'visible' => array( + 'input#twitter-toggle' => array( + 'checked' => TRUE, + ), ), ), - ), - ); - } - foreach ($form[$field_name][$langcode] as $delta => $field_instance) { - if (is_int($delta)) { - $form[$field_name][$langcode][$delta] += $element; + ); + } + foreach ($form[$field_name][$field_langcode] as $delta => $field_instance) { + if (is_int($delta)) { + $form[$field_name][$field_langcode][$delta] += $element; + } } } } diff --git a/twitter_post/twitter_post.install b/twitter_post/twitter_post.install index bd2ac3c..450c38c 100644 --- a/twitter_post/twitter_post.install +++ b/twitter_post/twitter_post.install @@ -10,16 +10,35 @@ function twitter_post_field_schema($field) { $columns = array(); $indexes = array(); - if ($field['type'] == 'twitter_post_tweet') { + if ($field['type'] == 'twitter_post') { $columns = array( 'status' => array( - 'type' => 'boolean', + 'description' => 'Control whether or not this entity will be tweeted.', + 'type' => 'int', + 'length' => 1, 'not null' => TRUE, + 'size' => 'tiny', + 'default' => 0, ), - 'format' => array( - 'type' => 'varchar', - 'length' => 150, - 'not null' => TRUE, + 'message' => array( + 'description' => "The text of the Twitter post.", + // Using a blob instead of a text type make it possible for MySQL to + // handle extended UTF8 characters, like emoji. + // @see https://www.drupal.org/node/1910376 + 'type' => 'blob', + // Balance size vs performance. The August 2015 update allows for DMs + // that are 10,000 characters in length, so in theory MySQL's default + // blob length of 16KB should be enough. + 'size' => 'normal', + 'not null' => FALSE, + ), + 'account' => array( + 'description' => "Unique identifier for the {twitter_account} this tweet is posting from to.", + 'type' => 'int', + 'unsigned' => TRUE, + 'size' => 'big', + 'not null' => FALSE, + 'default' => 0, ), ); $indexes = array(); @@ -89,3 +108,131 @@ function twitter_post_update_7500() { drupal_set_message(t('A new permission "Post a message to Twitter using a global account" as been added to all roles that already had the "Post a message to Twitter" permission. It is recommended to review the permissions to ensure they are appropriate for this site\'s needs.', array('@url' => url('admin/people/permissions')))); } + +/** + * Add the missing 'status' field. + */ +function twitter_post_update_7501() { + // Get the list of fields. + $types = array('twitter_post'); + $fields = array(); + foreach (field_info_fields() as $field) { + if (in_array($field['type'], $types)) { + $fields[] = $field; + } + } + + if (!empty($fields)) { + $spec = array( + 'description' => 'Control whether or not this entity will be tweeted.', + 'type' => 'int', + 'length' => 1, + 'not null' => TRUE, + 'size' => 'tiny', + 'default' => 0, + ); + + foreach ($fields as $field) { + $tables = array( + _field_sql_storage_tablename($field), + _field_sql_storage_revision_tablename($field), + ); + foreach ($tables as $table) { + $column = $field['field_name'] . '_status'; + if (db_field_exists($table, $column)) { + db_change_field($table, $column, $column, $spec); + } + else { + db_add_field($table, $column, $spec); + } + } + } + + return t('Added the missing "account" field.'); + } +} + +/** + * Change the 'message' field to a BLOB. + */ +function twitter_post_update_7502() { + // Get the list of fields. + $types = array('twitter_post'); + $fields = array(); + foreach (field_info_fields() as $field) { + if (in_array($field['type'], $types)) { + $fields[] = $field; + } + } + + if (!empty($fields)) { + $spec = array( + 'description' => "The text of the Twitter post.", + // Using a blob instead of a text type make it possible for MySQL to + // handle extended UTF8 characters, like emoji. + // @see https://www.drupal.org/node/1910376 + 'type' => 'blob', + // Balance size vs performance. The August 2015 update allows for DMs + // that are 10,000 characters in length, so in theory MySQL's default + // blob length of 16KB should be enough. + 'size' => 'normal', + 'not null' => FALSE, + ); + + foreach ($fields as $field) { + $tables = array( + _field_sql_storage_tablename($field), + _field_sql_storage_revision_tablename($field), + ); + foreach ($tables as $table) { + $column = $field['field_name'] . '_message'; + if (db_field_exists($table, $column)) { + db_change_field($table, $column, $column, $spec); + } + else { + db_add_field($table, $column, $spec); + } + } + } + + return t('Converted the "message" field to BLOB.'); + } +} + +/** + * Add the missing 'account' field. + */ +function twitter_post_update_7503() { + // Get the list of fields. + $types = array('twitter_post'); + $fields = array(); + foreach (field_info_fields() as $field) { + if (in_array($field['type'], $types)) { + $fields[] = $field; + } + } + + if (!empty($fields)) { + $spec = array( + 'description' => "Unique identifier for the {twitter_account} this tweet is posting from to.", + 'type' => 'int', + 'unsigned' => TRUE, + 'size' => 'big', + 'not null' => FALSE, + 'default' => 0, + ); + + foreach ($fields as $field) { + $tables = array( + _field_sql_storage_tablename($field), + _field_sql_storage_revision_tablename($field), + ); + foreach ($tables as $table) { + $column = $field['field_name'] . '_account'; + db_add_field($table, $column, $spec); + } + } + + return t('Added the missing "account" field.'); + } +} diff --git a/twitter_post/twitter_post.module b/twitter_post/twitter_post.module index 5a06be3..0c6193a 100644 --- a/twitter_post/twitter_post.module +++ b/twitter_post/twitter_post.module @@ -29,15 +29,16 @@ function twitter_post_permission() { * * Intercepts newly created entities and posts notices to Twitter. */ -function twitter_post_entity_insert($entity, $type) { +function twitter_post_entity_insert($entity, $entity_type) { // First we find twitter_post fields. - list($id, $vid, $bundle) = entity_extract_ids($type, $entity); - $fields_info = field_info_instances($type, $bundle); + list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); + $fields_info = field_info_instances($entity_type, $bundle); foreach ($fields_info as $field_name => $value) { $field_info = field_info_field($field_name); if ($field_info['type'] == 'twitter_post') { // Iterate field instances. - foreach ($entity->{$field_name}[$entity->language] as $delta => $field_instance) { + $items = field_get_items($entity_type, $entity, $field_name); + foreach ($items as $delta => $field_instance) { if (is_int($delta) && !empty($field_instance['account'])) { // Extract data out of each field. $status = $field_instance['status']; @@ -59,7 +60,7 @@ function twitter_post_entity_insert($entity, $type) { // Post to Twitter if the status checkbox is active. if ($status) { module_load_include('inc', 'twitter'); - $message = token_replace($message, array($type => $entity)); + $message = token_replace($message, array($entity_type => $entity)); $status = twitter_set_status($twitter_account, $message); if ($status) { drupal_set_message(t('Successfully posted "%node" to Twitter: @status', array(