summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpcambra2013-06-06 17:28:53 (GMT)
committer Juampy2013-06-06 17:28:53 (GMT)
commit2ae81e9f0ea5076498278592607adbd0b11724c2 (patch)
treeb25dc0efad1ff887e3c6e2c85665b3079aa59e34
parent0dd3166b383a9edd8d31ec3f09a36ad67178a321 (diff)
Issue #2011986 by pcambra, plopesc: [Drupal 8] Adapt the entity definitions.
-rw-r--r--lib/Drupal/twitter/Plugin/Core/Entity/TwitterAccount.php93
-rw-r--r--lib/Drupal/twitter/Plugin/Core/Entity/TwitterStatus.php77
-rw-r--r--lib/Drupal/twitter/TwitterAccountStorageController.php21
-rw-r--r--twitter.inc12
-rw-r--r--twitter.module128
-rw-r--r--twitter.pages.inc2
6 files changed, 198 insertions, 135 deletions
diff --git a/lib/Drupal/twitter/Plugin/Core/Entity/TwitterAccount.php b/lib/Drupal/twitter/Plugin/Core/Entity/TwitterAccount.php
new file mode 100644
index 0000000..f72d4d9
--- /dev/null
+++ b/lib/Drupal/twitter/Plugin/Core/Entity/TwitterAccount.php
@@ -0,0 +1,93 @@
+<?php
+
+/**
+ * @file
+ * Definition of Drupal\twitter\Plugin\Core\Entity\TwitterAccount.
+ */
+
+namespace Drupal\twitter\Plugin\Core\Entity;
+
+use Drupal\Core\Entity\EntityNG;
+use Drupal\Core\Entity\Annotation\EntityType;
+use Drupal\Core\Annotation\Translation;
+use Drupal\Core\Language\Language;
+
+/**
+ * Defines the twitter account entity class.
+ *
+ * @EntityType(
+ * id = "twitter_account",
+ * label = @Translation("Twitter account"),
+ * module = "twitter",
+ * controllers = {
+ * "storage" = "Drupal\twitter\TwitterAccountStorageController",
+ * "access" = "Drupal\twitter\TwitterAccountAccessController",
+ * "render" = "Drupal\Core\Entity\EntityRenderController",
+ * "form" = {
+ * "default" = "Drupal\twitter\TwitterAccountFormController",
+ * },
+ * "translation" = "Drupal\twitter\TwitterAccountTranslationController"
+ * },
+ * base_table = "twitter_account",
+ * uri_callback = "twitter_account_uri",
+ * label_callback = "twitter_account_label",
+ * fieldable = TRUE,
+ * translatable = TRUE,
+ * entity_keys = {
+ * "id" = "twitter_uid",
+ * "uuid" = "uuid"
+ * }
+ * )
+ */
+class TwitterAccount extends EntityNG {
+ /**
+ * Constructor
+ */
+ public function __construct($values = array()) {
+ // Prepare values to match twitter_account table fields.
+ if (!empty($values['id'])) {
+ $values['twitter_uid'] = $values['id'];
+ unset($values['id']);
+ }
+ if (!empty($values['created_at']) && $created_time = strtotime($values['created_at'])) {
+ $values['created_time'] = $created_time;
+ }
+
+ $values['utc_offset'] = isset($values['utc_offset']) ? $values['utc_offset'] : 0;
+ if (isset($values['status'])) {
+ $this->status = new TwitterStatus($values['status']);
+ unset($values['status']);
+ }
+ parent::__construct($values, 'twitter_account');
+ }
+
+ /**
+ * Returns an array with the authentication tokens.
+ *
+ * @return array with the oauth token key and secret.
+ */
+ public function getAuth() {
+ return array('oauth_token' => $this->oauth_token, 'oauth_token_secret' => $this->oauth_token_secret);
+ }
+
+ /**
+ * Sets the authentication tokens to a user.
+ *
+ * @param array $values
+ * Array with 'oauth_token' and 'oauth_token_secret' keys.
+ */
+ public function setAuth($values) {
+ $this->oauth_token = isset($values['oauth_token'])?$values['oauth_token']:NULL;
+ $this->oauth_token_secret = isset($values['oauth_token_secret'])?$values['oauth_token_secret']:NULL;
+ }
+
+ /**
+ * Checks whether the account is authenticated or not.
+ *
+ * @return
+ * boolean TRUE when the account is authenticated.
+ */
+ public function isAuth() {
+ return !empty($this->oauth_token) && !empty($this->oauth_token_secret);
+ }
+}
diff --git a/lib/Drupal/twitter/Plugin/Core/Entity/TwitterStatus.php b/lib/Drupal/twitter/Plugin/Core/Entity/TwitterStatus.php
new file mode 100644
index 0000000..323977b
--- /dev/null
+++ b/lib/Drupal/twitter/Plugin/Core/Entity/TwitterStatus.php
@@ -0,0 +1,77 @@
+<?php
+
+/**
+ * @file
+ * Definition of Drupal\twitter\Plugin\Core\Entity\TwitterStatus.
+ */
+
+namespace Drupal\twitter\Plugin\Core\Entity;
+
+use Drupal\Core\Entity\Entity;
+use Drupal\Core\Entity\Annotation\EntityType;
+use Drupal\Core\Annotation\Translation;
+use Drupal\Core\Language\Language;
+use Drupal\user\UserInterface;
+
+/**
+ * Defines the twitter status entity class.
+ *
+ * @EntityType(
+ * id = "twitter_status",
+ * label = @Translation("Individual Twitter statuses"),
+ * module = "twitter",
+ * controllers = {
+ * "storage" = "Drupal\twitter\TwitterStatusStorageController",
+ * "access" = "Drupal\twitter\TwitterStatusAccessController",
+ * "render" = "Drupal\Core\Entity\EntityRenderController",
+ * "form" = {
+ * "default" = "Drupal\twitter\TwitterStatusFormController",
+ * },
+ * "translation" = "Drupal\twitter\TwitterStatusTranslationController"
+ * },
+ * base_table = "twitter",
+ * uri_callback = "twitter_status_uri",
+ * label_callback = "twitter_status_label",
+ * fieldable = TRUE,
+ * translatable = TRUE,
+ * entity_keys = {
+ * "id" = "twitter_id",
+ * "uuid" = "uuid"
+ * }
+ * )
+ */
+class TwitterStatus extends EntityNG {
+ /**
+ * @var created_at
+ */
+ public $createdAt;
+
+ public $id;
+
+ public $text;
+
+ public $source;
+
+ public $truncated;
+
+ public $favorited;
+
+ public $inReplyToStatusId;
+
+ public $inReplyToUserId;
+
+ public $inReplyToScreenName;
+
+ public $user;
+
+ /**
+ * Constructor for TwitterStatus
+ */
+ public function __construct($values = array()) {
+ if (isset($values['user'])) {
+ $this->user = new TwitterAccount($values['user']);
+ unset($values['user']);
+ }
+ parent::__construct($values, 'twitter_status');
+ }
+}
diff --git a/lib/Drupal/twitter/TwitterAccountStorageController.php b/lib/Drupal/twitter/TwitterAccountStorageController.php
new file mode 100644
index 0000000..81d92b6
--- /dev/null
+++ b/lib/Drupal/twitter/TwitterAccountStorageController.php
@@ -0,0 +1,21 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\twitter\TwitterAccountStorageController.
+ */
+
+namespace Drupal\twitter;
+
+use Drupal\Core\Entity\DatabaseStorageControllerNG;
+use Drupal\Core\Entity\EntityInterface;
+
+/**
+ * Controller class for twitter acccounts
+ *
+ * This extends the Drupal\Core\Entity\DatabaseStorageController class, adding
+ * required special handling for twitter accounts.
+ */
+class TwitterAccountStorageController extends DatabaseStorageControllerNG {
+
+}
diff --git a/twitter.inc b/twitter.inc
index 64e764d..231756c 100644
--- a/twitter.inc
+++ b/twitter.inc
@@ -26,7 +26,7 @@ function twitter_connect($account = NULL) {
}
if ($account) {
- $auth = $account->get_auth();
+ $auth = $account->getAuth();
if (isset($auth['oauth_token']) && isset($auth['oauth_token_secret'])) {
$twitter_settings = config('twitter.settings');
return new Twitter($twitter_settings->('consumer_key'), $twitter_settings->get('consumer_secret'),
@@ -94,7 +94,7 @@ function twitter_load_authenticated_accounts() {
$accounts = twitter_load_accounts();
$auth_accounts = array();
foreach ($accounts as $index => $account) {
- if ($account->is_auth()) {
+ if ($account->isAuth()) {
$auth_accounts[] = $account;
}
}
@@ -108,12 +108,12 @@ function twitter_status_save($status) {
$twitter_status = new TwitterStatus();
$twitter_status->twitter_id = $status->id;
$twitter_status->screen_name = $status->user->screen_name;
- $twitter_status->created_time = strtotime($status->created_at);
+ $twitter_status->created_time = strtotime($status->createdAt);
$twitter_status->text = $status->text;
$twitter_status->source = $status->source;
- $twitter_status->in_reply_to_status_id = ($status->in_reply_to_status_id > 0) ? (string) $status->in_reply_to_status_id : NULL;
- $twitter_status->in_reply_to_user_id = (int) $status->in_reply_to_user_id;
- $twitter_status->in_reply_to_screen_name = $status->in_reply_to_screen_name;
+ $twitter_status->inReplyToStatusId = ($status->inReplyToStatusId > 0) ? (string) $status->inReplyToStatusId : NULL;
+ $twitter_status->inReplyToUserId = (int) $status->inReplyToUserId;
+ $twitter_status->inReplyToScreenName = $status->inReplyToScreenName;
$twitter_status->truncated = (int) $status->truncated;
$twitter_status->is_new = entity_load_single('twitter_status', $twitter_status->twitter_id) ? FALSE : TRUE;
entity_save('twitter_status', $twitter_status);
diff --git a/twitter.module b/twitter.module
index 1b45b51..54e8291 100644
--- a/twitter.module
+++ b/twitter.module
@@ -7,38 +7,6 @@
use Drupal\Core\Entity\Entity;
/**
- * Implements hook_entity_info().
- */
-function twitter_entity_info() {
- return array(
- 'twitter_status' => array(
- 'label' => t('Twitter Status'),
- 'module' => 'twitter',
- 'entity class' => 'TwitterStatus',
- 'controller class' => 'EntityAPIController',
- 'base table' => 'twitter',
- 'entity keys' => array(
- 'id' => 'twitter_id',
- ),
- 'label callback' => 'entity_class_label',
- 'uri callback' => 'entity_class_uri',
- ),
- 'twitter_account' => array(
- 'label' => t('Twitter Account'),
- 'module' => 'twitter',
- 'entity class' => 'TwitterAccount',
- 'controller class' => 'EntityAPIController',
- 'base table' => 'twitter_account',
- 'entity keys' => array(
- 'id' => 'twitter_uid',
- ),
- 'label callback' => 'entity_class_label',
- 'uri callback' => 'entity_class_uri',
- ),
- );
-}
-
-/**
* Implements hook_menu().
*/
function twitter_menu() {
@@ -416,99 +384,3 @@ function _twitter_user_profile($screen_name) {
function _twitter_status_url($status) {
return config('twitter.settings')->get('host') . '/' . $status->user->screen_name . '/status/' . $status->id;
}
-
-/**
- * Class for containing an individual twitter status.
- */
-class TwitterStatus extends Entity {
- /**
- * @var created_at
- */
- public $created_at;
-
- public $id;
-
- public $text;
-
- public $source;
-
- public $truncated;
-
- public $favorited;
-
- public $in_reply_to_status_id;
-
- public $in_reply_to_user_id;
-
- public $in_reply_to_screen_name;
-
- public $user;
-
- /**
- * Constructor for TwitterStatus
- */
- public function __construct($values = array()) {
- if (isset($values['user'])) {
- $this->user = new TwitterAccount($values['user']);
- unset($values['user']);
- }
- parent::__construct($values, 'twitter_status');
- }
-}
-
-/**
- * Class to define the structure of a Twitter account.
- */
-class TwitterAccount extends Entity {
- /**
- * Constructor
- */
- public function __construct($values = array()) {
- // Prepare values to match twitter_account table fields.
- if (!empty($values['id'])) {
- $values['twitter_uid'] = $values['id'];
- unset($values['id']);
- }
- if (!empty($values['created_at']) && $created_time = strtotime($values['created_at'])) {
- $values['created_time'] = $created_time;
- }
-
- $values['utc_offset'] = isset($values['utc_offset']) ? $values['utc_offset'] : 0;
- if (isset($values['status'])) {
- $this->status = new TwitterStatus($values['status']);
- unset($values['status']);
- }
- parent::__construct($values, 'twitter_account');
- }
-
- /**
- * Returns an array with the authentication tokens.
- *
- * @return
- * array with the oauth token key and secret.
- */
- public function get_auth() {
- return array('oauth_token' => $this->oauth_token, 'oauth_token_secret' => $this->oauth_token_secret);
- }
-
- /**
- * Sets the authentication tokens to a user.
- *
- * @param array $values
- * Array with 'oauth_token' and 'oauth_token_secret' keys.
- */
- public function set_auth($values) {
- $this->oauth_token = isset($values['oauth_token'])?$values['oauth_token']:NULL;
- $this->oauth_token_secret = isset($values['oauth_token_secret'])?$values['oauth_token_secret']:NULL;
- }
-
- /**
- * Checks whether the account is authenticated or not.
- *
- * @return
- * boolean TRUE when the account is authenticated.
- */
- public function is_auth() {
- return !empty($this->oauth_token) && !empty($this->oauth_token_secret);
- }
-}
diff --git a/twitter.pages.inc b/twitter.pages.inc
index 8d114a8..a35bf50 100644
--- a/twitter.pages.inc
+++ b/twitter.pages.inc
@@ -385,7 +385,7 @@ function twitter_oauth_callback_form_submit($form, &$form_state) {
return;
}
// Save the new Twitter account and set the user's uid who added it.
- $twitter_account->set_auth($response);
+ $twitter_account->setAuth($response);
global $user;
$twitter_account->uid = $user->uid;
twitter_account_save($twitter_account);