summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorplopesc2013-06-06 16:37:53 (GMT)
committer Juampy2013-06-06 16:37:53 (GMT)
commit0dd3166b383a9edd8d31ec3f09a36ad67178a321 (patch)
tree5af37b4ae163c19ef24fd4583649b6879451b5ea
parent314b123ab82922fca53747bae33846298dd6d039 (diff)
Issue #2012360 by plopesc: [Drupal 8]Convert twitter variables to config system.
-rw-r--r--config/twitter.settings.yml8
-rw-r--r--lib/Drupal/twitter/Form/TwitterSettingsForm.php50
-rw-r--r--tests/twitter_mock.module11
-rw-r--r--twitter.inc3
-rw-r--r--twitter.install22
-rw-r--r--twitter.lib.php10
-rw-r--r--twitter.module25
-rw-r--r--twitter.pages.inc94
-rw-r--r--twitter_views_field_handlers.inc4
9 files changed, 73 insertions, 154 deletions
diff --git a/config/twitter.settings.yml b/config/twitter.settings.yml
new file mode 100644
index 0000000..3996998
--- /dev/null
+++ b/config/twitter.settings.yml
@@ -0,0 +1,8 @@
+consumer_key: NULL
+consumer_secret: NULL
+api: 'https://api.twitter.com'
+host: 'http://twitter.com'
+tinyurl: 'http://tinyurl.com'
+import: TRUE
+expire: 0
+search: 'http://twitter.com/search'
diff --git a/lib/Drupal/twitter/Form/TwitterSettingsForm.php b/lib/Drupal/twitter/Form/TwitterSettingsForm.php
index 1c25f89..c925d9d 100644
--- a/lib/Drupal/twitter/Form/TwitterSettingsForm.php
+++ b/lib/Drupal/twitter/Form/TwitterSettingsForm.php
@@ -14,25 +14,26 @@ use Drupal\system\SystemConfigFormBase;
class TwitterSettingsForm extends SystemConfigFormBase {
/**
- * Implements \Drupal\Core\Form\FormInterface::getFormID().
+ * {@inheritdoc}
*/
public function getFormID() {
return 'twitter_settings_form';
}
/**
- * Implements \Drupal\Core\Form\FormInterface::buildForm().
+ * {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
- $form['twitter_import'] = array(
+ $twitter_settings = config('twitter.settings');
+ $form['import'] = array(
'#type' => 'checkbox',
'#title' => t('Import and display the Twitter statuses of site users who have entered their Twitter account information.'),
- '#default_value' => variable_get('twitter_import', 1),
+ '#default_value' => $twitter_settings->get('import'),
);
- $form['twitter_expire'] = array(
+ $form['expire'] = array(
'#type' => 'select',
'#title' => t('Delete old statuses'),
- '#default_value' => variable_get('twitter_expire', 0),
+ '#default_value' => $twitter_settings->get('expire'),
'#options' => array(0 => t('Never')) + drupal_map_assoc(array(604800, 2592000, 7776000, 31536000), 'format_interval'),
'#states' => array(
'visible' => array(
@@ -51,15 +52,15 @@ class TwitterSettingsForm extends SystemConfigFormBase {
'#title' => t('Callback URL'),
'#markup' => url('twitter/oauth', array('absolute' => TRUE)),
);
- $form['oauth']['twitter_consumer_key'] = array(
+ $form['oauth']['consumer_key'] = array(
'#type' => 'textfield',
'#title' => t('OAuth Consumer key'),
- '#default_value' => variable_get('twitter_consumer_key', NULL),
+ '#default_value' => $twitter_settings->get('consumer_key'),
);
- $form['oauth']['twitter_consumer_secret'] = array(
+ $form['oauth']['consumer_secret'] = array(
'#type' => 'textfield',
'#title' => t('OAuth Consumer secret'),
- '#default_value' => variable_get('twitter_consumer_secret', NULL),
+ '#default_value' => $twitter_settings->get('consumer_secret'),
);
// Twitter external APIs settings.
$form['twitter'] = array(
@@ -68,35 +69,44 @@ class TwitterSettingsForm extends SystemConfigFormBase {
'#description' => t('The following settings connect Twitter module with external APIs. ' .
'Change them if, for example, you want to use Identi.ca.'),
);
- $form['twitter']['twitter_host'] = array(
+ $form['twitter']['host'] = array(
'#type' => 'textfield',
'#title' => t('Twitter host'),
- '#default_value' => variable_get('twitter_host', TWITTER_HOST),
+ '#default_value' => $twitter_settings->get('host'),
);
- $form['twitter']['twitter_api'] = array(
+ $form['twitter']['api'] = array(
'#type' => 'textfield',
'#title' => t('Twitter API'),
- '#default_value' => variable_get('twitter_api', TWITTER_API),
+ '#default_value' => $twitter_settings->get('api'),
);
- $form['twitter']['twitter_search'] = array(
+ $form['twitter']['search'] = array(
'#type' => 'textfield',
'#title' => t('Twitter search'),
- '#default_value' => variable_get('twitter_search', TWITTER_SEARCH),
+ '#default_value' => $twitter_settings->get('search'),
);
- $form['twitter']['twitter_tinyurl'] = array(
+ $form['twitter']['tinyurl'] = array(
'#type' => 'textfield',
'#title' => t('TinyURL'),
- '#default_value' => variable_get('twitter_tinyurl', TWITTER_TINYURL),
+ '#default_value' => $twitter_settings->get('tinyurl'),
);
return parent::buildForm($form, $form_state);
}
/**
- * Implements \Drupal\Core\Form\FormInterface::submitForm().
+ * {@inheritdoc}
*/
public function submitForm(array &$form, array &$form_state) {
-
+ config('twitter.settings')
+ ->set('import', $form_state['values']['import'])
+ ->set('expire', $form_state['values']['expire'])
+ ->set('consumer_key', $form_state['values']['consumer_key'])
+ ->set('consumer_secret', $form_state['values']['consumer_secret'])
+ ->set('host', $form_state['values']['host'])
+ ->set('api', $form_state['values']['api'])
+ ->set('search', $form_state['values']['search'])
+ ->set('tinyurl', $form_state['values']['tinyurl'])
+ ->save();
parent::submitForm($form, $form_state);
}
diff --git a/tests/twitter_mock.module b/tests/twitter_mock.module
index 4a128a1..8cefafd 100644
--- a/tests/twitter_mock.module
+++ b/tests/twitter_mock.module
@@ -14,16 +14,7 @@
*/
function twitter_mock_enable() {
global $base_root;
- variable_set('twitter_api', $base_root);
-}
-
-/**
- * Implements hook_disable().
- *
- * Sets the Twitter API host back to its default value
- */
-function twitter_mock_disable() {
- variable_del('twitter_api');
+ config('twitter.settings')->set('api', $base_root)->save();
}
/**
diff --git a/twitter.inc b/twitter.inc
index 13652fe..64e764d 100644
--- a/twitter.inc
+++ b/twitter.inc
@@ -28,7 +28,8 @@ function twitter_connect($account = NULL) {
if ($account) {
$auth = $account->get_auth();
if (isset($auth['oauth_token']) && isset($auth['oauth_token_secret'])) {
- return new Twitter(variable_get('twitter_consumer_key', ''), variable_get('twitter_consumer_secret', ''),
+ $twitter_settings = config('twitter.settings');
+ return new Twitter($twitter_settings->('consumer_key'), $twitter_settings->get('consumer_secret'),
$auth['oauth_token'], $auth['oauth_token_secret']);
}
}
diff --git a/twitter.install b/twitter.install
index 9392922..7bd7fd9 100644
--- a/twitter.install
+++ b/twitter.install
@@ -14,8 +14,8 @@ function twitter_requirements($phase) {
if ($phase == 'runtime') {
// Verify that the Twitter Application keys are set.
$requirements['twitter_keys'] = array('title' => $t('Twitter Application keys'));
- $consumer_key = variable_get('twitter_consumer_key', NULL);
- $consumer_secret = variable_get('twitter_consumer_secret', NULL);
+ $consumer_key = config('twitter.settings')->get('consumer_key');
+ $consumer_secret = config('twitter.settings')->get('consumer_secret');
if (empty($consumer_key) || empty($consumer_secret)) {
$requirements['twitter_keys']['value'] = $t('Missing');
$requirements['twitter_keys']['description'] =
@@ -326,24 +326,6 @@ function twitter_install() {
}
/**
- * Implements hook_uninstall().
- */
-function twitter_uninstall() {
- // Remove variables
- variable_del('twitter_import');
- variable_del('twitter_expire');
- variable_del('twitter_consumer_key');
- variable_del('twitter_consumer_secret');
- variable_del('twitter_host');
- variable_del('twitter_signin_button');
- variable_del('twitter_signin_register');
- variable_del('twitter_host');
- variable_del('twitter_api');
- variable_del('twitter_search');
- variable_del('twitter_tinyurl');
-}
-
-/**
* Removes password field
*/
function twitter_update_7300() {
diff --git a/twitter.lib.php b/twitter.lib.php
index cf810ae..d26b3a2 100644
--- a/twitter.lib.php
+++ b/twitter.lib.php
@@ -45,7 +45,7 @@ class Twitter {
}
public function get_request_token() {
- $url = variable_get('twitter_api', TWITTER_API) . '/oauth/request_token';
+ $url = config('twitter.settings')->get('api') . '/oauth/request_token';
try {
$params = array('oauth_callback' => url('twitter/oauth', array('absolute' => TRUE)));
$response = $this->auth_request($url, $params);
@@ -60,14 +60,14 @@ class Twitter {
}
public function get_authorize_url($token) {
- $url = variable_get('twitter_api', TWITTER_API) . '/oauth/authorize';
+ $url = config('twitter.settings')->get('api') . '/oauth/authorize';
$url.= '?oauth_token=' . $token['oauth_token'];
return $url;
}
public function get_authenticate_url($token) {
- $url = variable_get('twitter_api', TWITTER_API) . '/oauth/authenticate';
+ $url = config('twitter.settings')->get('api') . '/oauth/authenticate';
$url.= '?oauth_token=' . $token['oauth_token'];
return $url;
@@ -83,7 +83,7 @@ class Twitter {
* String the access token or FALSE when there was an error.
*/
public function get_access_token($oauth_verifier = NULL) {
- $url = variable_get('twitter_api', TWITTER_API) . '/oauth/access_token';
+ $url = config('twitter.settings')->get('api') . '/oauth/access_token';
// Adding parameter oauth_verifier to auth_request
$parameters = array();
@@ -190,7 +190,7 @@ class Twitter {
* The complete path to the endpoint.
*/
protected function create_url($path, $format = '.json') {
- $url = variable_get('twitter_api', TWITTER_API) .'/1.1/'. $path . $format;
+ $url = config('twitter.settings')->get('api') .'/1.1/'. $path . $format;
return $url;
}
diff --git a/twitter.module b/twitter.module
index c6310cc..1b45b51 100644
--- a/twitter.module
+++ b/twitter.module
@@ -6,11 +6,6 @@
use Drupal\Core\Entity\Entity;
-define ('TWITTER_HOST', 'http://twitter.com');
-define ('TWITTER_API', 'https://api.twitter.com');
-define ('TWITTER_SEARCH', 'http://search.twitter.com');
-define ('TWITTER_TINYURL', 'http://tinyurl.com');
-
/**
* Implements hook_entity_info().
*/
@@ -170,7 +165,8 @@ function twitter_shorten_url($url) {
return shorten_url($url);
}
else {
- $response = drupal_http_request(variable_get('twitter_tinyurl', TWITTER_TINYURL) . "/api-create.php?url=" . $url);
+
+ $response = drupal_http_request(config('twitter.settings')->get('tinyurl') . "/api-create.php?url=" . $url);
if ($response->code == 200) {
return $response->data;
}
@@ -186,7 +182,7 @@ function twitter_shorten_url($url) {
* Imports new Twitter statuses for site users, and deletes expired tweets.
*/
function twitter_cron() {
- if (!variable_get('twitter_import', TRUE)) {
+ if (!config('twitter_settings')->get('import')) {
return;
}
// Check if we can connect to Twitter before proceeding.
@@ -227,7 +223,7 @@ function twitter_cron() {
}
// Nuke old statuses.
- if ($age = variable_get('twitter_expire', 0)) {
+ if ($age = config('twitter.settings')->get('expire')) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'twitter_account')
->propertyCondition('created_time', REQUEST_TIME - $age, '<');
@@ -294,7 +290,7 @@ function _twitter_filter_tip_link($filter, $format, $long = FALSE) {
*/
function _twitter_filter_username($text, $filter) {
$prefix = '@';
- $destination = variable_get('twitter_host', TWITTER_HOST) . '/';
+ $destination = config('twitter.settings')->get('host') . '/';
return _twitter_filter_text($text, $prefix, $destination);
}
@@ -303,7 +299,7 @@ function _twitter_filter_username($text, $filter) {
*/
function _twitter_filter_hashtag($text, $filter) {
$prefix = '#';
- $destination = variable_get('twitter_search', TWITTER_SEARCH) . '/search?q=%23';
+ $destination = config('twitter.settings')->get('search') . '/search?q=%23';
return _twitter_filter_text($text, $prefix, $destination);
}
@@ -398,8 +394,9 @@ function twitter_user_view_alter(&$build, \Drupal\user\Plugin\Core\Entity\User $
* boolean TRUE if both the Twitter Application key and secret are set.
*/
function twitter_api_keys() {
- $key = variable_get('twitter_consumer_key');
- $secret = variable_get('twitter_consumer_secret');
+ $twitter_settings = config('twitter.settings');
+ $key = $twitter_settings->get('consumer_key');
+ $secret = $twitter_settings->get('consumer_secret');
return !(empty($key) && empty($secret));
}
@@ -407,7 +404,7 @@ function twitter_api_keys() {
* Helper to build a Twitter profile URL
*/
function _twitter_user_profile($screen_name) {
- return l('@' . $screen_name, TWITTER_HOST . '/' . $screen_name);
+ return l('@' . $screen_name, config('twitter.settings')->get('host') . '/' . $screen_name);
}
/**
@@ -417,7 +414,7 @@ function _twitter_user_profile($screen_name) {
* A TwitterStatus object.
*/
function _twitter_status_url($status) {
- return TWITTER_HOST . '/' . $status->user->screen_name . '/status/' . $status->id;
+ return config('twitter.settings')->get('host') . '/' . $status->user->screen_name . '/status/' . $status->id;
}
/**
diff --git a/twitter.pages.inc b/twitter.pages.inc
index 3840a0c..8d114a8 100644
--- a/twitter.pages.inc
+++ b/twitter.pages.inc
@@ -5,80 +5,6 @@
*/
/**
- * Twitter settings form.
- */
-function twitter_admin_form($form, &$form_state) {
- $form['twitter_import'] = array(
- '#type' => 'checkbox',
- '#title' => t('Import and display the Twitter statuses of site users who have entered their Twitter account information.'),
- '#default_value' => variable_get('twitter_import', 1),
- );
- $form['twitter_expire'] = array(
- '#type' => 'select',
- '#title' => t('Delete old statuses'),
- '#default_value' => variable_get('twitter_expire', 0),
- '#options' => array(0 => t('Never')) + drupal_map_assoc(array(604800, 2592000, 7776000, 31536000), 'format_interval'),
- '#states' => array(
- 'visible' => array(
- ':input[name=twitter_import]' => array('checked' => TRUE),
- ),
- ),
- );
-
- $form['oauth'] = array(
- '#type' => 'fieldset',
- '#title' => t('OAuth Settings'),
- '#access' => module_exists('oauth_common'),
- '#description' => t('To enable OAuth based access for twitter, you must <a href="@url">register your application</a> with Twitter and add the provided keys here.', array('@url' => 'https://dev.twitter.com/apps/new')),
- );
- $form['oauth']['callback_url'] = array(
- '#type' => 'item',
- '#title' => t('Callback URL'),
- '#markup' => url('twitter/oauth', array('absolute' => TRUE)),
- );
- $form['oauth']['twitter_consumer_key'] = array(
- '#type' => 'textfield',
- '#title' => t('OAuth Consumer key'),
- '#default_value' => variable_get('twitter_consumer_key', NULL),
- );
- $form['oauth']['twitter_consumer_secret'] = array(
- '#type' => 'textfield',
- '#title' => t('OAuth Consumer secret'),
- '#default_value' => variable_get('twitter_consumer_secret', NULL),
- );
-
- // Twitter external APIs settings.
- $form['twitter'] = array(
- '#type' => 'fieldset',
- '#title' => t('Twitter Settings'),
- '#description' => t('The following settings connect Twitter module with external APIs. ' .
- 'Change them if, for example, you want to use Identi.ca.'),
- );
- $form['twitter']['twitter_host'] = array(
- '#type' => 'textfield',
- '#title' => t('Twitter host'),
- '#default_value' => variable_get('twitter_host', TWITTER_HOST),
- );
- $form['twitter']['twitter_api'] = array(
- '#type' => 'textfield',
- '#title' => t('Twitter API'),
- '#default_value' => variable_get('twitter_api', TWITTER_API),
- );
- $form['twitter']['twitter_search'] = array(
- '#type' => 'textfield',
- '#title' => t('Twitter search'),
- '#default_value' => variable_get('twitter_search', TWITTER_SEARCH),
- );
- $form['twitter']['twitter_tinyurl'] = array(
- '#type' => 'textfield',
- '#title' => t('TinyURL'),
- '#default_value' => variable_get('twitter_tinyurl', TWITTER_TINYURL),
- );
-
- return system_settings_form($form);
-}
-
-/**
* Form builder that lists Twitter accounts.
*
* @param object $account
@@ -322,8 +248,9 @@ function twitter_auth_account_form($form, $form_state) {
* Form validation for adding a new Twitter account.
*/
function twitter_auth_account_form_validate($form, &$form_state) {
- $key = variable_get('twitter_consumer_key', '');
- $secret = variable_get('twitter_consumer_secret', '');
+ $twitter_settings = config('twitter.settings');
+ $key = $twitter_settings->get('consumer_key');
+ $secret = $twitter_settings->get('consumer_secret');
if ($key == '' || $secret == '') {
form_set_error('', t('Please configure your consumer key and secret key at ' .
'<a href="!url">Twitter settings</a>.', array( '!url' => url('admin/config/services/twitter'),
@@ -337,8 +264,9 @@ function twitter_auth_account_form_validate($form, &$form_state) {
* Loads Twitter account details and adds them to the user account
*/
function twitter_auth_account_form_submit($form, &$form_state) {
- $key = variable_get('twitter_consumer_key', '');
- $secret = variable_get('twitter_consumer_secret', '');
+ $twitter_settings = config('twitter.settings');
+ $key = $twitter_settings->get('consumer_key');
+ $secret = $twitter_settings->get('consumer_secret');
$twitter = new Twitter($key, $secret);
$token = $twitter->get_request_token();
if ($token) {
@@ -398,8 +326,9 @@ function twitter_oauth_callback_form($form, &$form_state) {
* Validate results from Twitter OAuth return request.
*/
function twitter_oauth_callback_form_validate($form, &$form_state) {
- $key = variable_get('twitter_consumer_key', '');
- $secret = variable_get('twitter_consumer_secret', '');
+ $twitter_settings = config('twitter.settings');
+ $key = $twitter_settings->get('consumer_key');
+ $secret = $twitter_settings->get('consumer_secret');
if (isset($_SESSION['twitter_oauth'])) {
$form_state['twitter_oauth'] = $_SESSION['twitter_oauth'];
@@ -443,8 +372,9 @@ function twitter_oauth_callback_form_validate($form, &$form_state) {
* in the DB.
*/
function twitter_oauth_callback_form_submit($form, &$form_state) {
- $key = variable_get('twitter_consumer_key', '');
- $secret = variable_get('twitter_consumer_secret', '');
+ $twitter_settings = config('twitter.settings');
+ $key = $twitter_settings->get('consumer_key');
+ $secret = $twitter_settings->get('consumer_secret');
$response = $form_state['twitter_oauth']['response'];
$twitter = new Twitter($key, $secret, $response['oauth_token'], $response['oauth_token_secret']);
diff --git a/twitter_views_field_handlers.inc b/twitter_views_field_handlers.inc
index d651a41..302758a 100644
--- a/twitter_views_field_handlers.inc
+++ b/twitter_views_field_handlers.inc
@@ -13,7 +13,7 @@ class twitter_views_handler_field_xss extends views_handler_field {
$options['link_urls'] = array('default' => TRUE);
$options['link_usernames'] = array('default' => TRUE);
$options['link_hashtags'] = array('default' => FALSE);
- $options['hashtags_url'] = array('default' => variable_get('twitter_search', TWITTER_SEARCH) . '/search?q=%23');
+ $options['hashtags_url'] = array('default' => config('twitter.settings')->get('search') . '/search?q=%23');
$options['link_attributes'] = array('default' => TRUE);
return $options;
}
@@ -62,7 +62,7 @@ class twitter_views_handler_field_xss extends views_handler_field {
}
// Link usernames with their profiles.
if (!empty($this->options['link_usernames'])) {
- $value = _twitter_filter_text($value, '@', variable_get('twitter_host', TWITTER_HOST) . '/');
+ $value = _twitter_filter_text($value, '@', config('twitter.settings')->get('host') . '/');
}
// Link hashtags.
if (!empty($this->options['link_hashtags']) && valid_url($this->options['hashtags_url'])) {