From f934a50f3a53a7937148c048a9107377b4edcbad Mon Sep 17 00:00:00 2001 From: John Doyle Date: Tue, 7 Jul 2015 20:29:33 -0400 Subject: [PATCH 01/77] Delete LICENSE Renamed to LICENSE.txt --- LICENSE | 340 -------------------------------------------------------- 1 file changed, 340 deletions(-) delete mode 100644 LICENSE diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 8cdb845..0000000 --- a/LICENSE +++ /dev/null @@ -1,340 +0,0 @@ - GNU GENERAL PUBLIC LICENSE - Version 2, June 1991 - - Copyright (C) 1989, 1991 Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The licenses for most software are designed to take away your -freedom to share and change it. By contrast, the GNU General Public -License is intended to guarantee your freedom to share and change free -software--to make sure the software is free for all its users. This -General Public License applies to most of the Free Software -Foundation's software and to any other program whose authors commit to -using it. (Some other Free Software Foundation software is covered by -the GNU Lesser General Public License instead.) You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -this service if you wish), that you receive source code or can get it -if you want it, that you can change the software or use pieces of it -in new free programs; and that you know you can do these things. - - To protect your rights, we need to make restrictions that forbid -anyone to deny you these rights or to ask you to surrender the rights. -These restrictions translate to certain responsibilities for you if you -distribute copies of the software, or if you modify it. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must give the recipients all the rights that -you have. You must make sure that they, too, receive or can get the -source code. And you must show them these terms so they know their -rights. - - We protect your rights with two steps: (1) copyright the software, and -(2) offer you this license which gives you legal permission to copy, -distribute and/or modify the software. - - Also, for each author's protection and ours, we want to make certain -that everyone understands that there is no warranty for this free -software. If the software is modified by someone else and passed on, we -want its recipients to know that what they have is not the original, so -that any problems introduced by others will not reflect on the original -authors' reputations. - - Finally, any free program is threatened constantly by software -patents. We wish to avoid the danger that redistributors of a free -program will individually obtain patent licenses, in effect making the -program proprietary. To prevent this, we have made it clear that any -patent must be licensed for everyone's free use or not licensed at all. - - The precise terms and conditions for copying, distribution and -modification follow. - - GNU GENERAL PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - 0. This License applies to any program or other work which contains -a notice placed by the copyright holder saying it may be distributed -under the terms of this General Public License. The "Program", below, -refers to any such program or work, and a "work based on the Program" -means either the Program or any derivative work under copyright law: -that is to say, a work containing the Program or a portion of it, -either verbatim or with modifications and/or translated into another -language. (Hereinafter, translation is included without limitation in -the term "modification".) Each licensee is addressed as "you". - -Activities other than copying, distribution and modification are not -covered by this License; they are outside its scope. The act of -running the Program is not restricted, and the output from the Program -is covered only if its contents constitute a work based on the -Program (independent of having been made by running the Program). -Whether that is true depends on what the Program does. - - 1. You may copy and distribute verbatim copies of the Program's -source code as you receive it, in any medium, provided that you -conspicuously and appropriately publish on each copy an appropriate -copyright notice and disclaimer of warranty; keep intact all the -notices that refer to this License and to the absence of any warranty; -and give any other recipients of the Program a copy of this License -along with the Program. - -You may charge a fee for the physical act of transferring a copy, and -you may at your option offer warranty protection in exchange for a fee. - - 2. You may modify your copy or copies of the Program or any portion -of it, thus forming a work based on the Program, and copy and -distribute such modifications or work under the terms of Section 1 -above, provided that you also meet all of these conditions: - - a) You must cause the modified files to carry prominent notices - stating that you changed the files and the date of any change. - - b) You must cause any work that you distribute or publish, that in - whole or in part contains or is derived from the Program or any - part thereof, to be licensed as a whole at no charge to all third - parties under the terms of this License. - - c) If the modified program normally reads commands interactively - when run, you must cause it, when started running for such - interactive use in the most ordinary way, to print or display an - announcement including an appropriate copyright notice and a - notice that there is no warranty (or else, saying that you provide - a warranty) and that users may redistribute the program under - these conditions, and telling the user how to view a copy of this - License. (Exception: if the Program itself is interactive but - does not normally print such an announcement, your work based on - the Program is not required to print an announcement.) - -These requirements apply to the modified work as a whole. If -identifiable sections of that work are not derived from the Program, -and can be reasonably considered independent and separate works in -themselves, then this License, and its terms, do not apply to those -sections when you distribute them as separate works. But when you -distribute the same sections as part of a whole which is a work based -on the Program, the distribution of the whole must be on the terms of -this License, whose permissions for other licensees extend to the -entire whole, and thus to each and every part regardless of who wrote it. - -Thus, it is not the intent of this section to claim rights or contest -your rights to work written entirely by you; rather, the intent is to -exercise the right to control the distribution of derivative or -collective works based on the Program. - -In addition, mere aggregation of another work not based on the Program -with the Program (or with a work based on the Program) on a volume of -a storage or distribution medium does not bring the other work under -the scope of this License. - - 3. You may copy and distribute the Program (or a work based on it, -under Section 2) in object code or executable form under the terms of -Sections 1 and 2 above provided that you also do one of the following: - - a) Accompany it with the complete corresponding machine-readable - source code, which must be distributed under the terms of Sections - 1 and 2 above on a medium customarily used for software interchange; or, - - b) Accompany it with a written offer, valid for at least three - years, to give any third party, for a charge no more than your - cost of physically performing source distribution, a complete - machine-readable copy of the corresponding source code, to be - distributed under the terms of Sections 1 and 2 above on a medium - customarily used for software interchange; or, - - c) Accompany it with the information you received as to the offer - to distribute corresponding source code. (This alternative is - allowed only for noncommercial distribution and only if you - received the program in object code or executable form with such - an offer, in accord with Subsection b above.) - -The source code for a work means the preferred form of the work for -making modifications to it. For an executable work, complete source -code means all the source code for all modules it contains, plus any -associated interface definition files, plus the scripts used to -control compilation and installation of the executable. However, as a -special exception, the source code distributed need not include -anything that is normally distributed (in either source or binary -form) with the major components (compiler, kernel, and so on) of the -operating system on which the executable runs, unless that component -itself accompanies the executable. - -If distribution of executable or object code is made by offering -access to copy from a designated place, then offering equivalent -access to copy the source code from the same place counts as -distribution of the source code, even though third parties are not -compelled to copy the source along with the object code. - - 4. You may not copy, modify, sublicense, or distribute the Program -except as expressly provided under this License. Any attempt -otherwise to copy, modify, sublicense or distribute the Program is -void, and will automatically terminate your rights under this License. -However, parties who have received copies, or rights, from you under -this License will not have their licenses terminated so long as such -parties remain in full compliance. - - 5. You are not required to accept this License, since you have not -signed it. However, nothing else grants you permission to modify or -distribute the Program or its derivative works. These actions are -prohibited by law if you do not accept this License. Therefore, by -modifying or distributing the Program (or any work based on the -Program), you indicate your acceptance of this License to do so, and -all its terms and conditions for copying, distributing or modifying -the Program or works based on it. - - 6. Each time you redistribute the Program (or any work based on the -Program), the recipient automatically receives a license from the -original licensor to copy, distribute or modify the Program subject to -these terms and conditions. You may not impose any further -restrictions on the recipients' exercise of the rights granted herein. -You are not responsible for enforcing compliance by third parties to -this License. - - 7. If, as a consequence of a court judgment or allegation of patent -infringement or for any other reason (not limited to patent issues), -conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot -distribute so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you -may not distribute the Program at all. For example, if a patent -license would not permit royalty-free redistribution of the Program by -all those who receive copies directly or indirectly through you, then -the only way you could satisfy both it and this License would be to -refrain entirely from distribution of the Program. - -If any portion of this section is held invalid or unenforceable under -any particular circumstance, the balance of the section is intended to -apply and the section as a whole is intended to apply in other -circumstances. - -It is not the purpose of this section to induce you to infringe any -patents or other property right claims or to contest validity of any -such claims; this section has the sole purpose of protecting the -integrity of the free software distribution system, which is -implemented by public license practices. Many people have made -generous contributions to the wide range of software distributed -through that system in reliance on consistent application of that -system; it is up to the author/donor to decide if he or she is willing -to distribute software through any other system and a licensee cannot -impose that choice. - -This section is intended to make thoroughly clear what is believed to -be a consequence of the rest of this License. - - 8. If the distribution and/or use of the Program is restricted in -certain countries either by patents or by copyrighted interfaces, the -original copyright holder who places the Program under this License -may add an explicit geographical distribution limitation excluding -those countries, so that distribution is permitted only in or among -countries not thus excluded. In such case, this License incorporates -the limitation as if written in the body of this License. - - 9. The Free Software Foundation may publish revised and/or new versions -of the General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - -Each version is given a distinguishing version number. If the Program -specifies a version number of this License which applies to it and "any -later version", you have the option of following the terms and conditions -either of that version or of any later version published by the Free -Software Foundation. If the Program does not specify a version number of -this License, you may choose any version ever published by the Free Software -Foundation. - - 10. If you wish to incorporate parts of the Program into other free -programs whose distribution conditions are different, write to the author -to ask for permission. For software which is copyrighted by the Free -Software Foundation, write to the Free Software Foundation; we sometimes -make exceptions for this. Our decision will be guided by the two goals -of preserving the free status of all derivatives of our free software and -of promoting the sharing and reuse of software generally. - - NO WARRANTY - - 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY -FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN -OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES -PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED -OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS -TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE -PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, -REPAIR OR CORRECTION. - - 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR -REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, -INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING -OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED -TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY -YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER -PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE -POSSIBILITY OF SUCH DAMAGES. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -convey the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - {description} - Copyright (C) {year} {fullname} - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - -Also add information on how to contact you by electronic and paper mail. - -If the program is interactive, make it output a short notice like this -when it starts in an interactive mode: - - Gnomovision version 69, Copyright (C) year name of author - Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, the commands you use may -be called something other than `show w' and `show c'; they could even be -mouse-clicks or menu items--whatever suits your program. - -You should also get your employer (if you work as a programmer) or your -school, if any, to sign a "copyright disclaimer" for the program, if -necessary. Here is a sample; alter the names: - - Yoyodyne, Inc., hereby disclaims all copyright interest in the program - `Gnomovision' (which makes passes at compilers) written by James Hacker. - - {signature of Ty Coon}, 1 April 1989 - Ty Coon, President of Vice - -This General Public License does not permit incorporating your program into -proprietary programs. If your program is a subroutine library, you may -consider it more useful to permit linking proprietary applications with the -library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. - From 152d646ed56509b1706b15105afc97c0c00462e1 Mon Sep 17 00:00:00 2001 From: John Doyle Date: Tue, 7 Jul 2015 20:55:51 -0400 Subject: [PATCH 02/77] Delete README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index 80f90f6..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# addthis -Port AddThis to D8 From 54c03481cf0001bc92a5d9bc340e4f713c32a9ee Mon Sep 17 00:00:00 2001 From: Doyle Date: Wed, 8 Jul 2015 09:27:02 -0400 Subject: [PATCH 03/77] Added libraries for addthis JS and CSS. Added permissions for advanced settings form. Added new menu item/router for advanced settings form. Implemented advanced settings form. Added new schema/config file for advanced settings. Began work to port AddThisScriptManager --- addthis.libraries.yml | 12 + addthis.links.menu.yml | 7 +- addthis.module | 6 + addthis.permissions.yml | 3 + addthis.routing.yml | 7 + config/install/addthis.settings.advanced.yml | 9 + config/schema/addthis.schema.yml | 32 ++- css/addthis.admin.css | 11 + js/addthis.js | 98 +++++++ src/AddThis.php | 14 +- src/Form/AddThisSettingsAdvancedForm.php | 143 +++++++++++ src/Form/AddThisSettingsForm.php | 3 + src/Plugin/Block/AddThisBlock.php | 6 +- .../Field/FieldFormatter/AddThisFormatter.php | 10 +- src/Services/AddThisScriptManager.php | 239 ++++++++++++++++++ 15 files changed, 592 insertions(+), 8 deletions(-) create mode 100644 addthis.libraries.yml create mode 100644 addthis.permissions.yml create mode 100644 config/install/addthis.settings.advanced.yml create mode 100644 css/addthis.admin.css create mode 100644 js/addthis.js create mode 100644 src/Form/AddThisSettingsAdvancedForm.php create mode 100644 src/Services/AddThisScriptManager.php diff --git a/addthis.libraries.yml b/addthis.libraries.yml new file mode 100644 index 0000000..7a5e05c --- /dev/null +++ b/addthis.libraries.yml @@ -0,0 +1,12 @@ +addthis.admin: + version: VERSION + css: + theme: + css/addthis.admin.css: {} + http://cache.addthiscdn.com/icons/v1/sprites/services.css: { type: external } +addthis.global: + version: VERSION + js: + js/addthis.js: { scope: footer } + + diff --git a/addthis.links.menu.yml b/addthis.links.menu.yml index f35fea7..9e001f8 100644 --- a/addthis.links.menu.yml +++ b/addthis.links.menu.yml @@ -2,4 +2,9 @@ addthis.settings: title: AddThis description: 'Manage your site''s AddThis settings.' parent: system.admin_config_ui - route_name: addthis.settings \ No newline at end of file + route_name: addthis.settings +addthis.settings: + title: AddThis + description: 'Manage your site''s Advanced AddThis settings.' + parent: addthis.settings + route_name: addthis.settings.advanced \ No newline at end of file diff --git a/addthis.module b/addthis.module index a4abe2d..4134d5b 100644 --- a/addthis.module +++ b/addthis.module @@ -1,2 +1,8 @@ getServicesCssUrl(), 'external'); - drupal_add_css($this->getAdminCssFilePath(), 'file'); - } public function getFullBookmarkUrl() { return $this->getBaseBookmarkUrl() . $this->getProfileIdQueryParameterPrefixedWithAmp(); @@ -246,4 +242,14 @@ private function getProfileIdQueryParameterPrefixedWithHash() { } + /* + * Get markup for a given display type. + * + * When $options does not contain #entity, link to the current URL. + * When $options does not contain #display, use default settings. + */ + public function getDisplayMarkup($display, $options = array()) { + return array(); + } + } \ No newline at end of file diff --git a/src/Form/AddThisSettingsAdvancedForm.php b/src/Form/AddThisSettingsAdvancedForm.php new file mode 100644 index 0000000..958bfe7 --- /dev/null +++ b/src/Form/AddThisSettingsAdvancedForm.php @@ -0,0 +1,143 @@ + 'fieldset', + '#title' => t('Service URLs'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + $form['service_urls_fieldset']['addthis_bookmark_url'] = array( + '#type' => 'textfield', + '#title' => t('AddThis bookmark URL'), + '#default_value' => $config->get('addthis_bookmark_url'), + '#required' => TRUE, + ); + $form['service_urls_fieldset']['addthis_services_css_url'] = array( + '#type' => 'textfield', + '#title' => t('AddThis services stylesheet URL'), + '#default_value' => $config->get('addthis_services_css_url'), + '#required' => TRUE, + ); + $form['service_urls_fieldset']['addthis_services_json_url'] = array( + '#type' => 'textfield', + '#title' => t('AddThis services json URL'), + '#default_value' => $config->get('addthis_services_json_url'), + '#required' => TRUE, + ); + $form['service_urls_fieldset']['addthis_widget_js_url'] = array( + '#type' => 'textfield', + '#title' => t('AddThis javascript widget URL'), + '#default_value' => $config->get('addthis_widget_js_url'), + '#required' => TRUE, + ); + + // Advanced settings. + $form['advanced_settings_fieldset'] = array( + '#type' => 'fieldset', + '#title' => t('Advanced settings'), + '#access' => \Drupal::currentUser()->hasPermission('administer advanced addthis'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + $form['advanced_settings_fieldset']['addthis_custom_configuration_code_enabled'] = array( + '#type' => 'checkbox', + '#title' => t('Use custom AddThis configuration code'), + '#default_value' => $config->get('addthis_custom_configuration_code_enabled'), + '#required' => FALSE, + '#description' => t('Use custom AddThis configuration code. If checked, custom configuration will be used instead of other configuration settings provided in AddThis administration user interface.'), + ); + $form['advanced_settings_fieldset']['addthis_custom_configuration_code'] = array( + '#type' => 'textarea', + '#title' => t('AddThis custom configuration code'), + '#default_value' => $config->get('addthis_custom_configuration_code'), + '#required' => FALSE, + '#description' => t('AddThis custom configuration code. See format at AddThis.com'), + ); + $form['advanced_settings_fieldset']['addthis_widget_load_domready'] = array( + '#type' => 'checkbox', + '#title' => t('Load the AddThis resources after the DOM is ready.'), + '#default_value' => $config->get('addthis_widget_load_domready'), + ); + $form['advanced_settings_fieldset']['addthis_widget_load_async'] = array( + '#type' => 'checkbox', + '#title' => t('Initialize asynchronously through addthis.init().'), + '#description' => t('Use this when you have your own Ajax functionality or create things after the DOM is ready trough Javascript. Initialize the addthis functionality through addthis.init().'), + '#default_value' => $config->get('addthis_widget_load_async'), + ); + $form['advanced_settings_fieldset']['addthis_widget_include'] = array( + '#type' => 'select', + '#title' => t('Load widget js.'), + '#options' => array( + '0' => t('Don\'t include at all.'), + '1' => t('Include on all (non admin) pages'), + '2' => t('(Default) Include on widget rendering by Drupal.'), + ), + '#default_value' => $config->get('addthis_widget_include'), + ); + + + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $this->config('addthis.settings.advanced') + ->set('addthis_bookmark_url', $form_state->getValue('addthis_bookmark_url')) + ->set('addthis_services_css_url_key', $form_state->getValue('addthis_services_css_url_key')) + ->set('addthis_services_json_url_key', $form_state->getValue('addthis_services_json_url_key')) + ->set('addthis_widget_js_url', $form_state->getValue('addthis_widget_js_url')) + ->set('addthis_custom_configuration_code_enabled', $form_state->getValue('addthis_custom_configuration_code_enabled')) + ->set('addthis_custom_configuration_code', $form_state->getValue('addthis_custom_configuration_code')) + ->set('addthis_widget_load_domready', $form_state->getValue('addthis_widget_load_domready')) + ->set('addthis_widget_load_async', $form_state->getValue('addthis_widget_load_async')) + ->set('addthis_widget_include', $form_state->getValue('addthis_widget_include')) + ->save(); + + parent::submitForm($form, $form_state); + } + +} + diff --git a/src/Form/AddThisSettingsForm.php b/src/Form/AddThisSettingsForm.php index 43c22cb..e1f8d56 100644 --- a/src/Form/AddThisSettingsForm.php +++ b/src/Form/AddThisSettingsForm.php @@ -37,6 +37,9 @@ protected function getEditableConfigNames() { public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('addthis.settings'); + //Add our library to the settings form to add in custom CSS. + $form['#attached']['library'][] = 'addthis/addthis.admin'; + // Visual settings. $form['fieldset_compact_menu'] = array( '#type' => 'fieldset', diff --git a/src/Plugin/Block/AddThisBlock.php b/src/Plugin/Block/AddThisBlock.php index b752933..e015c74 100644 --- a/src/Plugin/Block/AddThisBlock.php +++ b/src/Plugin/Block/AddThisBlock.php @@ -28,9 +28,13 @@ class AddThisBlock extends BlockBase public function build() { //@TODO Implement block markup + + $widget_type = AddThis::getInstance()->getBlockDisplayType(); + $widget_settings = AddThis::getInstance()->getBlockDisplaySettings(); + $markup = 'Testing Markup'; return array( - '#markup' => $markup, + '#markup' => array(), ); } diff --git a/src/Plugin/Field/FieldFormatter/AddThisFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisFormatter.php index 0052c4c..eaa2a4d 100644 --- a/src/Plugin/Field/FieldFormatter/AddThisFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AddThisFormatter.php @@ -17,7 +17,7 @@ * id = "addthis_disabled", * label = @Translation("AddThis Disabled"), * field_types = { -* "disabled" +* "addthis_disabled" * } * ) */ @@ -27,6 +27,14 @@ class AddThisFormatter extends FormatterBase { */ public function viewElements(FieldItemListInterface $items) { //@TODO Implement viewElements() + $element = array(); + $display_type = 'addthis_disabled'; + + + + $markup = array( + '#display' => array(), + ); } } \ No newline at end of file diff --git a/src/Services/AddThisScriptManager.php b/src/Services/AddThisScriptManager.php new file mode 100644 index 0000000..7d9cb5a --- /dev/null +++ b/src/Services/AddThisScriptManager.php @@ -0,0 +1,239 @@ +addthis = AddThis::getInstance(); + + $this->async = \Drupal::config('addthis.settings.advanced')->get('addthis_widget_load_async'); + $this->domready = \Drupal::config('addthis.settings.advanced')->get('addthis_widget_load_domready'); + } + + /** + * Return a single instance of the AddThisScriptManager. + * + * @return AddThisScriptManager + */ + public static function getInstance() { + static $manager; + + if (!isset($manager)) { + $manager = new AddThisScriptManager(); + } + return $manager; + } + + /** + * Get the current widget js url. + * + * @return string + * A url reference to the widget js. + */ + public function getWidgetJsUrl() { + return check_url(\Drupal::config('addthis.settings.advanced')->get('addthis_widget_js_url')); + } + + /** + * Return if we are on https connection. + * + * @return bool + * TRUE if the current request is on https. + */ + public function isHttps() { + global $is_https; + + return $is_https; + } + + /** + * Change the schema from http to https if we are on https. + * + * @param string $url + * A full url. + * + * @return string + * The changed url. + */ + public function correctSchemaIfHttps($url) { + if (is_string($url) && $this->isHttps()) { + return str_replace('http://', 'https://', $url); + } + else { + return $url; + } + throw new InvalidArgumentException('The argument was not a string value'); + } + + /** + * Attach the widget js to the element. + * + * @todo Change the scope of the addthis.js. + * See if we can get the scope of the addthis.js into the header + * just below the settings so that the settings can be used in the loaded + * addthis.js of our module. + * + * @param array $element + * The element to attach the JavaScript to. + */ + public function attachJsToElement(&$element) { + $config = \Drupal::config('addthis.settings'); + $adv_config = \Drupal::config('addthis.settings.advanced'); + if ($adv_config->get('addthis_widget_js_include') != AddThis::WIDGET_JS_INCLUDE_NONE) { + $widget_js = new AddThisWidgetJsUrl($adv_config->get('addthis_widget_js_url')); + + $pubid = $config->get('analytics.addthis_profile_id'); + if (isset($pubid) && !empty($pubid) && is_string($pubid)) { + $widget_js->addAttribute('pubid', $pubid); + } + + $async = $this->async; + if ($async) { + $widget_js->addAttribute('async', 1); + } + + $domready = $this->domready; + if ($domready) { + $widget_js->addAttribute('domready', 1); + } + + // Only when the script is not loaded after the DOM is ready we include + // the script with #attached. + if (!$domready) { + $element['#attached']['js'][$this->getWidgetJsUrl()] = array( + 'type' => 'external', + 'scope' => 'footer', + ); + } + + // Every setting value passed here overrides previously set values but + // leaves the values that are already set somewhere else and that are not + // passed here. + $element['#attached']['js'][] = array( + 'type' => 'setting', + 'data' => array( + 'addthis' => array( + 'async' => $async, + 'domready' => $domready, + 'widget_url' => $this->getWidgetJsUrl(), + + 'addthis_config' => $this->getJsAddThisConfig(), + 'addthis_share' => $this->getJsAddThisShare(), + ) + ) + ); + } + } + + /** + * Enable / disable domready loading. + * + * @param bool $enabled + * TRUE to enabled domready loading. + */ + function setDomReady($enabled) { + $this->domready = $enabled; + } + + /** + * Enable / disable async loading. + * + * @param bool $enabled + * TRUE to enabled async loading. + */ + function setAsync($enabled) { + $this->async = $enabled; + } + + /** + * Get a array with all addthis_config values. + * + * Allow alter through 'addthis_configuration'. + * + * @todo Add static cache. + * + * @todo Make the adding of configuration dynamic. + * SRP is lost here. + */ + private function getJsAddThisConfig() { + global $language; + + $enabled_services = $this->addthis->getServiceNamesAsCommaSeparatedString($this->addthis->getEnabledServices()) . 'more'; + $excluded_services = $this->addthis->getServiceNamesAsCommaSeparatedString($this->addthis->getExcludedServices()); + + $configuration = array( + 'pubid' => $this->addthis->getProfileId(), + 'services_compact' => $enabled_services, + 'services_exclude' => $excluded_services, + 'data_track_clickback' => $this->addthis->isClickbackTrackingEnabled(), + 'ui_508_compliant' => $this->addthis->get508Compliant(), + 'ui_click' => $this->addthis->isClickToOpenCompactMenuEnabled(), + 'ui_cobrand' => $this->addthis->getCoBrand(), + 'ui_delay' => $this->addthis->getUiDelay(), + 'ui_header_background' => $this->addthis->getUiHeaderBackgroundColor(), + 'ui_header_color' => $this->addthis->getUiHeaderColor(), + 'ui_open_windows' => $this->addthis->isOpenWindowsEnabled(), + 'ui_use_css' => $this->addthis->isStandardCssEnabled(), + 'ui_use_addressbook' => $this->addthis->isAddressbookEnabled(), + 'ui_language' => $language->language, + ); + if (module_exists('googleanalytics')) { + if ($this->addthis->isGoogleAnalyticsTrackingEnabled()) { + $configuration['data_ga_property'] = variable_get('googleanalytics_account', ''); + $configuration['data_ga_social'] = $this->addthis->isGoogleAnalyticsSocialTrackingEnabled(); + } + } + + drupal_alter('addthis_configuration', $configuration); + return $configuration; + } + + /** + * Get a array with all addthis_share values. + * + * Allow alter through 'addthis_configuration_share'. + * + * @todo Add static cache. + * + * @todo Make the adding of configuration dynamic. + * SRP is lost here. + */ + private function getJsAddThisShare() { + + $configuration = $this->getJsAddThisConfig(); + + if (isset($configuration['templates'])) { + $addthis_share = array( + 'templates' => $configuration['templates'], + ); + } + $addthis_share['templates']['twitter'] = $this->addthis->getTwitterTemplate(); + + drupal_alter('addthis_configuration_share', $configuration); + return $addthis_share; + } + +} From 7e6738303636c6897aa7f0d94364d5932fcaaba8 Mon Sep 17 00:00:00 2001 From: Doyle Date: Thu, 9 Jul 2015 08:37:56 -0400 Subject: [PATCH 04/77] Ported AddThisScriptManager and updated JS to work with D8. Attached drupalSettings and external JS to fit with the new system. Began work on the addthis_displays module. --- addthis.libraries.yml | 10 ++ addthis.module | 10 ++ addthis_displays/addthis_displays.info.yml | 11 ++ addthis_displays/addthis_displays.module | 156 +++++++++++++++++++++ js/addthis.js | 21 ++- src/Services/AddThisScriptManager.php | 89 ++++++------ src/Util/AddThisWidgetJsUrl.php | 53 +++++++ 7 files changed, 298 insertions(+), 52 deletions(-) create mode 100644 addthis_displays/addthis_displays.info.yml create mode 100644 addthis_displays/addthis_displays.module create mode 100644 src/Util/AddThisWidgetJsUrl.php diff --git a/addthis.libraries.yml b/addthis.libraries.yml index 7a5e05c..6adb29c 100644 --- a/addthis.libraries.yml +++ b/addthis.libraries.yml @@ -4,9 +4,19 @@ addthis.admin: theme: css/addthis.admin.css: {} http://cache.addthiscdn.com/icons/v1/sprites/services.css: { type: external } + addthis.global: version: VERSION js: js/addthis.js: { scope: footer } + dependencies: + - core/jquery + - core/drupal + - core/drupalSettings + +addthis.widget: + version: VERSION + js: + http://s7.addthis.com/js/300/addthis_widget.js: { type: external, scope: footer } diff --git a/addthis.module b/addthis.module index 4134d5b..dbb7581 100644 --- a/addthis.module +++ b/addthis.module @@ -4,5 +4,15 @@ function addthis_page_attachments(array &$page){ $page['#attached']['library'][] = 'addthis/addthis.global'; +// Include the widget js when we include on all but admin pages. + //if (!path_is_admin(current_path()) && AddThis::getInstance()->getWidgetJsInclude() == AddThis::WIDGET_JS_INCLUDE_PAGE) { + $manager = \Drupal\addthis\Services\AddThisScriptManager::getInstance(); + $manager->attachJsToElement($page); + //} + +} + + +function addthis_library_info_alter(){ } \ No newline at end of file diff --git a/addthis_displays/addthis_displays.info.yml b/addthis_displays/addthis_displays.info.yml new file mode 100644 index 0000000..87ffbe9 --- /dev/null +++ b/addthis_displays/addthis_displays.info.yml @@ -0,0 +1,11 @@ +name: AddThis Displays +description: Adds the AddThis display types to render several basic types of AddThis buttons. +type: module +core: 8.x +package: Sharing + +dependencies: + - addthis + + + diff --git a/addthis_displays/addthis_displays.module b/addthis_displays/addthis_displays.module new file mode 100644 index 0000000..b64a15b --- /dev/null +++ b/addthis_displays/addthis_displays.module @@ -0,0 +1,156 @@ +. + '#theme' => 'addthis_wrapper', + '#tag' => 'div', + '#attributes' => array( + 'class' => array( + 'addthis_toolbox', + 'addthis_default_style', + ($options['#display']['settings']['buttons_size'] == AddThis::CSS_32x32 ? AddThis::CSS_32x32 : NULL), + $options['#display']['settings']['extra_css'], + ), + ), + ); + $element['#attributes'] += $addthis->getAddThisAttributesMarkup($options); + + // Add the widget script. + $script_manager = AddThisScriptManager::getInstance(); + $script_manager->attachJsToElement($element); + + $services = trim($options['#display']['settings']['share_services']); + $services = str_replace(' ', '', $services); + $services = explode(',', $services); + + // All service elements + $items = array(); + foreach ($services as $service) { + $items[$service] = array( + '#theme' => 'addthis_element', + '#tag' => 'a', + '#value' => '', + '#attributes' => array( + 'href' => $addthis->getBaseBookmarkUrl(), + 'class' => array( + 'addthis_button_' . $service, + ), + ), + '#addthis_service' => $service, + ); + + // Add individual counters. + if (strpos($service, 'counter_') === 0) { + $items[$service]['#attributes']['class'] = array("addthis_$service"); + } + + // Basic implementations of bubble counter orientation. + // @todo Figure all the bubbles out and add them. + // Still missing: tweetme, hyves and stubleupon, google_plusone_badge. + // + $orientation = ($options['#display']['settings']['counter_orientation'] == 'horizontal' ? TRUE : FALSE); + switch ($service) { + case 'linkedin_counter': + $items[$service]['#attributes'] += array( + 'li:counter' => ($orientation ? '' : 'top'), + ); + break; + case 'facebook_like': + $items[$service]['#attributes'] += array( + 'fb:like:layout' => ($orientation ? 'button_count' : 'box_count') + ); + break; + case 'facebook_share': + $items[$service]['#attributes'] += array( + 'fb:share:layout' => ($orientation ? 'button_count' : 'box_count') + ); + break; + case 'google_plusone': + $items[$service]['#attributes'] += array( + 'g:plusone:size' => ($orientation ? 'standard' : 'tall') + ); + break; + case 'tweet': + $items[$service]['#attributes'] += array( + 'tw:count' => ($orientation ? 'horizontal' : 'vertical'), + 'tw:via' => AddThis::getInstance()->getTwitterVia(), + ); + break; + case 'bubble_style': + $items[$service]['#attributes']['class'] = array( + 'addthis_counter', 'addthis_bubble_style' + ); + break; + case 'pill_style': + $items[$service]['#attributes']['class'] = array( + 'addthis_counter', 'addthis_pill_style' + ); + break; + } + } + $element += $items; + + return $element; +} + +/** + * Implements hook_addthis_display_markup__[display](). + */ +function addthis_displays_addthis_display_markup__addthis_basic_button($options = array()) { + + $addthis = AddThis::getInstance(); + $settings = $options['#display']['settings']; + + $button_img = 'http://s7.addthis.com/static/btn/sm-share-en.gif'; + if (isset($settings['buttons_size']) && $settings['buttons_size'] == 'big') { + $button_img = 'http://s7.addthis.com/static/btn/v2/lg-share-en.gif'; + } + $button_img = $addthis->transformToSecureUrl($button_img); + + $extra_css = isset($settings['extra_css']) ? $settings['extra_css'] : ''; + $element = array( + '#theme' => 'addthis_wrapper', + '#tag' => 'a', + '#attributes' => array( + 'class' => array( + 'addthis_button', + $extra_css, + ), + ), + ); + $element['#attributes'] += $addthis->getAddThisAttributesMarkup($options); + + // Add the widget script. + $script_manager = AddThisScriptManager::getInstance(); + $script_manager->attachJsToElement($element); + + // Create img button. + $image = array( + '#theme' => 'addthis_element', + '#tag' => 'img', + '#attributes' => array( + 'src' => $button_img, + 'alt' => t('Share page with AddThis'), + ), + ); + $element[] = $image; + + return $element; +} diff --git a/js/addthis.js b/js/addthis.js index f1487bd..6861a89 100644 --- a/js/addthis.js +++ b/js/addthis.js @@ -3,11 +3,10 @@ * AddThis javascript actions. */ -(function ($) { +(function ($, Drupal, drupalSettings) { Drupal.behaviors.addthis = { scriptLoaded: false, - - attach: function(context, settings) { + attach: function(context) { // The addthis configuration is not loaded but the settings are passed // to do so. @@ -35,7 +34,7 @@ // Returns if the settings defined by the addthis module are loaded. isSettingsLoaded: function () { - if (typeof Drupal.settings.addthis == 'undefined') { + if (typeof drupalSettings.addthis == 'undefined') { return false; } return true; @@ -50,8 +49,8 @@ }, initConfig: function () { - addthis_config = Drupal.settings.addthis.addthis_config; - addthis_share = Drupal.settings.addthis.addthis_share; + addthis_config = drupalSettings.addthis.addthis_config; + addthis_share = drupalSettings.addthis.addthis_share; }, // Load the js library when the dom is ready. @@ -60,8 +59,8 @@ // load the script here. if (!this.scriptLoaded && this.isConfigLoaded() && - Drupal.settings.addthis.domready) { - $.getScript(Drupal.settings.addthis.widget_url, Drupal.behaviors.addthis.scriptReady); + drupalSettings.addthis.domready) { + $.getScript(drupalSettings.addthis.widget_url, Drupal.behaviors.addthis.scriptReady); } }, @@ -83,8 +82,8 @@ // This load the config in time to run any addthis functionality. if (Drupal.behaviors.addthis.isConfigLoaded()) { - addthis_config = Drupal.settings.addthis.addthis_config; - addthis_share = Drupal.settings.addthis.addthis_share; + addthis_config = drupalSettings.addthis.addthis_config; + addthis_share = drupalSettings.addthis.addthis_share; } // Document ready in case we want to load AddThis into the dom after every @@ -95,4 +94,4 @@ Drupal.behaviors.addthis.loadDomready(); }); -}(jQuery)); +}(jQuery, Drupal, drupalSettings)); diff --git a/src/Services/AddThisScriptManager.php b/src/Services/AddThisScriptManager.php index 7d9cb5a..4c2834d 100644 --- a/src/Services/AddThisScriptManager.php +++ b/src/Services/AddThisScriptManager.php @@ -15,11 +15,11 @@ namespace Drupal\addthis\Services; -use Drupal\addthis\Addthis; +use Drupal\addthis\Util\AddThisWidgetJsUrl; + class AddThisScriptManager { - private $addthis = NULL; private $async = NULL; private $domready = NULL; @@ -27,8 +27,6 @@ class AddThisScriptManager { * Construct method. */ private function __construct() { - $this->addthis = AddThis::getInstance(); - $this->async = \Drupal::config('addthis.settings.advanced')->get('addthis_widget_load_async'); $this->domready = \Drupal::config('addthis.settings.advanced')->get('addthis_widget_load_domready'); } @@ -102,7 +100,7 @@ public function correctSchemaIfHttps($url) { public function attachJsToElement(&$element) { $config = \Drupal::config('addthis.settings'); $adv_config = \Drupal::config('addthis.settings.advanced'); - if ($adv_config->get('addthis_widget_js_include') != AddThis::WIDGET_JS_INCLUDE_NONE) { + if ($adv_config->get('addthis_widget_js_include') !== 0) { $widget_js = new AddThisWidgetJsUrl($adv_config->get('addthis_widget_js_url')); $pubid = $config->get('analytics.addthis_profile_id'); @@ -123,27 +121,18 @@ public function attachJsToElement(&$element) { // Only when the script is not loaded after the DOM is ready we include // the script with #attached. if (!$domready) { - $element['#attached']['js'][$this->getWidgetJsUrl()] = array( - 'type' => 'external', - 'scope' => 'footer', - ); + $element['#attached']['library'][] = 'addthis/addthis.widget'; } // Every setting value passed here overrides previously set values but // leaves the values that are already set somewhere else and that are not // passed here. - $element['#attached']['js'][] = array( - 'type' => 'setting', - 'data' => array( - 'addthis' => array( - 'async' => $async, - 'domready' => $domready, - 'widget_url' => $this->getWidgetJsUrl(), - - 'addthis_config' => $this->getJsAddThisConfig(), - 'addthis_share' => $this->getJsAddThisShare(), - ) - ) + $element['#attached']['drupalSettings']['addthis'] = array( + 'async' => $async, + 'domready' => $domready, + 'widget_url' => $this->getWidgetJsUrl(), + 'addthis_config' => $this->getJsAddThisConfig(), + 'addthis_share' => $this->getJsAddThisShare(), ); } } @@ -180,34 +169,35 @@ function setAsync($enabled) { */ private function getJsAddThisConfig() { global $language; + $config = \Drupal::config('addthis.settings'); - $enabled_services = $this->addthis->getServiceNamesAsCommaSeparatedString($this->addthis->getEnabledServices()) . 'more'; - $excluded_services = $this->addthis->getServiceNamesAsCommaSeparatedString($this->addthis->getExcludedServices()); + $enabled_services = $this->getServiceNamesAsCommaSeparatedString($config->get('compact_menu.enabled_services.addthis_enabled_services')) . 'more'; + $excluded_services = $this->getServiceNamesAsCommaSeparatedString($config->get('excluded_services.addthis_excluded_services')); $configuration = array( - 'pubid' => $this->addthis->getProfileId(), + 'pubid' => $config->get('analytics.addthis_profile_id'), 'services_compact' => $enabled_services, 'services_exclude' => $excluded_services, - 'data_track_clickback' => $this->addthis->isClickbackTrackingEnabled(), - 'ui_508_compliant' => $this->addthis->get508Compliant(), - 'ui_click' => $this->addthis->isClickToOpenCompactMenuEnabled(), - 'ui_cobrand' => $this->addthis->getCoBrand(), - 'ui_delay' => $this->addthis->getUiDelay(), - 'ui_header_background' => $this->addthis->getUiHeaderBackgroundColor(), - 'ui_header_color' => $this->addthis->getUiHeaderColor(), - 'ui_open_windows' => $this->addthis->isOpenWindowsEnabled(), - 'ui_use_css' => $this->addthis->isStandardCssEnabled(), - 'ui_use_addressbook' => $this->addthis->isAddressbookEnabled(), + 'data_track_clickback' => $config->get('analytics.addthis_clickback_tracking_enabled'), + 'ui_508_compliant' => $config->get('compact_menu.additionals.addthis_508_compliant'), + 'ui_click' => $config->get('compact_menu.menu_style.addthis_click_to_open_compact_menu_enabled'), + 'ui_cobrand' => $config->get('compact_menu.menu_style.addthis_co_brand'), + 'ui_delay' => $config->get('compact_menu.menu_style.addthis_ui_delay'), + 'ui_header_background' => $config->get('compact_menu.menu_style.addthis_ui_header_background_color'), + 'ui_header_color' => $config->get('compact_menu.menu_style.addthis_ui_header_color'), + 'ui_open_windows' => $config->get('compact_menu.menu_style.addthis_open_windows_enabled'), + 'ui_use_css' => $config->get('compact_menu.additionals.addthis_standard_css_enabled'), + 'ui_use_addressbook' => $config->get('compact_menu.additionals.addthis_addressbook_enabled'), 'ui_language' => $language->language, ); - if (module_exists('googleanalytics')) { - if ($this->addthis->isGoogleAnalyticsTrackingEnabled()) { - $configuration['data_ga_property'] = variable_get('googleanalytics_account', ''); - $configuration['data_ga_social'] = $this->addthis->isGoogleAnalyticsSocialTrackingEnabled(); + if (\Drupal::moduleHandler()->moduleExists('googleanalytics')) { + if ($config->get('analytics.addthis_google_analytics_tracking_enabled')) { + $configuration['data_ga_property'] = \Drupal::config(google_analytics.settings)->get('google_analytics_account'); + $configuration['data_ga_social'] = $config->get('analytics.addthis_google_analytics_social_tracking_enabled'); } } - drupal_alter('addthis_configuration', $configuration); + // drupal_alter('addthis_configuration', $configuration); return $configuration; } @@ -230,10 +220,27 @@ private function getJsAddThisShare() { 'templates' => $configuration['templates'], ); } - $addthis_share['templates']['twitter'] = $this->addthis->getTwitterTemplate(); + $addthis_share['templates']['twitter'] = \Drupal::config('addthis.settings')->get('third_party.addthis_twitter_template'); - drupal_alter('addthis_configuration_share', $configuration); + //drupal_alter('addthis_configuration_share', $configuration); return $addthis_share; } + /** + * Returns a comma separated list of service values. + * + * @param $services + * @return string + */ + public function getServiceNamesAsCommaSeparatedString($services) { + $serviceNames = array_values($services); + $servicesAsCommaSeparatedString = ''; + foreach ($serviceNames as $serviceName) { + if ($serviceName != '0') { + $servicesAsCommaSeparatedString .= $serviceName . ','; + } + } + return $servicesAsCommaSeparatedString; + } + } diff --git a/src/Util/AddThisWidgetJsUrl.php b/src/Util/AddThisWidgetJsUrl.php new file mode 100644 index 0000000..a6dac88 --- /dev/null +++ b/src/Util/AddThisWidgetJsUrl.php @@ -0,0 +1,53 @@ +url = $url; + } + + /** + * Add a attribute to the querystring. + */ + public function addAttribute($name, $value) { + $this->attributes[$name] = $value; + } + + /** + * Remove a attribute from the querystring. + */ + public function removeAttribute($name) { + if (isset($attributes[$name])) { + unset($attributes[$name]); + } + } + + /** + * Get the full url for the widgetjs. + */ + public function getFullUrl() { + $querystring_elements = array(); + foreach ($this->attributes as $key => $value) { + if (!empty($value)) { + $querystring_elements[] = $key . '=' . $value; + } + } + + $querystring = implode('&', $querystring_elements); + + return $this->url . '#' . $querystring; + } + +} From bb0921d8f5dd47e4cd8ca23c6db2d4f357886793 Mon Sep 17 00:00:00 2001 From: Doyle Date: Wed, 5 Aug 2015 09:04:59 -0400 Subject: [PATCH 05/77] Added stubs for basic formatters. Upgraded some additional helper functions. --- addthis.module | 73 +++++++++++ src/AddThis.php | 34 ++++- src/Plugin/Block/AddThisBlock.php | 71 ++++++++++- .../AddThisBasicButtonFormatter.php | 110 ++++++++++++++++ .../AddThisBasicToolboxFormatter.php | 119 ++++++++++++++++++ .../Field/FieldFormatter/AddThisFormatter.php | 2 +- 6 files changed, 404 insertions(+), 5 deletions(-) create mode 100644 src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php create mode 100644 src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php diff --git a/addthis.module b/addthis.module index dbb7581..670fb0e 100644 --- a/addthis.module +++ b/addthis.module @@ -1,5 +1,23 @@ array( + 'render element' => 'element', + ), + 'addthis_element' => array( + 'render element' => 'element', + ), +// 'addthis' => array( +// 'render element' => 'element', +// ), + ); +} + + function addthis_page_attachments(array &$page){ $page['#attached']['library'][] = 'addthis/addthis.global'; @@ -15,4 +33,59 @@ function addthis_page_attachments(array &$page){ function addthis_library_info_alter(){ +} + + +/** + * Implementation to retrieve formatters for a given type of field. + */ +function _addthis_field_info_formatter_field_type($field_type = NULL) { + $formatters = \Drupal::Service('plugin.manager.field.formatter')->getDefinitions(); + foreach ($formatters as $key => $formatter) { + if (!in_array((!isset($field_type) ? 'addthis' : $field_type), $formatter['field types'])) { + unset($formatters[$key]); + } + } + return $formatters; +} + + + +function theme_addthis_wrapper($variables) { + $element = $variables['addthis_wrapper']; + $output = '<' . $element['#tag'] . drupal_attributes($element['#attributes']) . '>'; + $children = element_children($element); + + if (count($children) > 0) { + foreach ($children as $child) { + $output .= render($element[$child]); + } + } + + $output .= '\n"; + return $output; +} + +/** + * Theme the elements that are created in the AddThis module. + * + * This is created with hook_addthis_element. + */ +function theme_addthis_element($variables) { + $element = $variables['addthis_element']; + + if (!isset($element['#value'])) { + return '<' . $element['#tag'] . drupal_attributes($element['#attributes']) . " />\n"; + } + + $output = '<' . $element['#tag'] . drupal_attributes($element['#attributes']) . '>'; + if (isset($element['#value_prefix'])) { + $output .= $element['#value_prefix']; + } + $output .= $element['#value']; + if (isset($element['#value_suffix'])) { + $output .= $element['#value_suffix']; + } + $output .= '\n"; + return $output; } \ No newline at end of file diff --git a/src/AddThis.php b/src/AddThis.php index d7807f7..5edcd11 100644 --- a/src/AddThis.php +++ b/src/AddThis.php @@ -242,6 +242,14 @@ private function getProfileIdQueryParameterPrefixedWithHash() { } + public function getDisplayTypes() { + $displays = array(); + foreach ($display_impl = _addthis_field_info_formatter_field_type() as $key => $display) { + $displays[$key] = t(SafeMarkup::checkPlain($display['label'])); + } + return $displays; + } + /* * Get markup for a given display type. * @@ -249,7 +257,31 @@ private function getProfileIdQueryParameterPrefixedWithHash() { * When $options does not contain #display, use default settings. */ public function getDisplayMarkup($display, $options = array()) { - return array(); + $formatters = \Drupal::Service('plugin.manager.field.formatter')->getDefinitions(); + + if (!array_key_exists($display, $formatters)) { + return array(); + } + // The display type exists. Now get it and get the markup. + $display_information = $formatters[$display]; + + // Theme function might only give a display name and + // render on default implementation. + if (!isset($options['#display']) || + (isset($options['#display']['type']) && $options['#display']['type'] != $display)) { + + $options['#display'] = isset($options['#display']) ? $options['#display'] : array(); + $options['#display'] = array_merge($options['#display'], $display_information); + $options['#display']['type'] = $display; + + } + + + $markup = array( + '#display' => $options['#display'], + ); + + return $markup; } } \ No newline at end of file diff --git a/src/Plugin/Block/AddThisBlock.php b/src/Plugin/Block/AddThisBlock.php index e015c74..9160fb4 100644 --- a/src/Plugin/Block/AddThisBlock.php +++ b/src/Plugin/Block/AddThisBlock.php @@ -9,6 +9,7 @@ use Drupal\Core\Block\BlockBase; use Drupal\Core\Form\FormStateInterface; use Drupal\addthis\AddThis; +use Drupal\addthis\Services\AddThisScriptManager; /** * Provides my custom block. @@ -22,6 +23,15 @@ class AddThisBlock extends BlockBase { + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return array( + 'type' => 'addthis_disabled', + ); + } + /** * {@inheritdoc} */ @@ -32,18 +42,73 @@ public function build() $widget_type = AddThis::getInstance()->getBlockDisplayType(); $widget_settings = AddThis::getInstance()->getBlockDisplaySettings(); - $markup = 'Testing Markup'; + $markup = AddThis::getInstance()->getDisplayMarkup('addthis_basic_button'); + $formatters = \Drupal::Service('plugin.manager.field.formatter')->getDefinitions(); + //$markup = 'Testing Markup'; + + + $button_img = 'http://s7.addthis.com/static/btn/sm-share-en.gif'; + $element = array( + '#theme' => 'addthis_wrapper', + '#tag' => 'a', + '#attributes' => array( + 'class' => array( + 'addthis_button', + ), + ), + ); + + $test = drupal_render($element); + + // Add the widget script. + $script_manager = AddThisScriptManager::getInstance(); + $script_manager->attachJsToElement($element); + + // Create img button. + $image = array( + '#theme' => 'addthis_element', + '#tag' => 'img', + '#attributes' => array( + 'src' => $button_img, + 'alt' => t('Share page with AddThis'), + ), + ); + + $element[] = $image; + + $markup = render($element); return array( - '#markup' => array(), + 'content' => $markup, ); } function blockForm($form, FormStateInterface $form_state) { //@TODO: Implement block form. + $form['settings']['addthis_settings'] = array( + '#type' => 'fieldset', + '#title' => 'Display settings', + ); + + // Retrieve settings. + $addthis_type = AddThis::getInstance()->getBlockDisplayType(); + $addthis_settings = AddThis::getInstance()->getBlockDisplaySettings(); + + + // The list of formatters. + $formatter_options = AddThis::getInstance()->getDisplayTypes(); + + $form['settings']['addthis_settings']['type'] = array( + '#type' => 'select', + '#title' => t('Formatter for @title', array('@title' => 'AddThis block')), + '#title_display' => 'invisible', + '#options' => $formatter_options, + '#default_value' => $this->configuration['type'], + '#attributes' => array('class' => array('addthis-display-type')), + ); return $form; } public function blockSubmit($form, FormStateInterface $form_state) { - //@TODO: Implement block submit. + $this->configuration['type'] = $form_state->getValue('type'); } } diff --git a/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php new file mode 100644 index 0000000..56bf1e0 --- /dev/null +++ b/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php @@ -0,0 +1,110 @@ + 'small', + 'extra_css' => '', + ) + parent::defaultSettings(); + } + + /** + * {@inheritdoc} + */ + public function settingsForm(array $form, FormStateInterface $form_state) { + $element = array(); + + $element['button_size'] = array( + '#title' => t('Image'), + '#type' => 'select', + '#default_value' => $this->getSetting('button_size'), + '#options' => array( + 'small' => t('Small'), + 'big' => t('Big'), + ), + ); + $element['extra_css'] = array( + '#title' => t('Extra CSS declaration'), + '#type' => 'textfield', + '#size' => 40, + '#default_value' => $this->getSetting('extra_css'), + '#description' => t('Specify extra CSS classes to apply to the button'), + ); + + return $element; + } + + + /** + * {@inheritdoc} + */ + public function viewElements(FieldItemListInterface $items) { + //@TODO Implement viewElements() + $addthis = AddThis::getInstance(); + //$settings = $options['#display']['settings']; + + $button_img = 'http://s7.addthis.com/static/btn/sm-share-en.gif'; + if (isset($settings['buttons_size']) && $settings['buttons_size'] == 'big') { + $button_img = 'http://s7.addthis.com/static/btn/v2/lg-share-en.gif'; + } + //$button_img = $addthis->transformToSecureUrl($button_img); + + //$extra_css = isset($settings['extra_css']) ? $settings['extra_css'] : ''; + $element = array( + '#theme' => 'addthis_wrapper', + '#tag' => 'a', + '#attributes' => array( + 'class' => array( + 'addthis_button', + ), + ), + ); + //$element['#attributes'] += $addthis->getAddThisAttributesMarkup($options); + + // Add the widget script. + $script_manager = AddThisScriptManager::getInstance(); + $script_manager->attachJsToElement($element); + + // Create img button. + $image = array( + '#theme' => 'addthis_element', + '#tag' => 'img', + '#attributes' => array( + 'src' => $button_img, + 'alt' => t('Share page with AddThis'), + ), + ); + $element[] = $image; + + return $element; + } + + + + +} \ No newline at end of file diff --git a/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php new file mode 100644 index 0000000..f1ec0dc --- /dev/null +++ b/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php @@ -0,0 +1,119 @@ + 'facebook,twitter', + 'buttons_size' => 'addthis_16x16_style', + 'counter_orientation' => 'horizontal', + 'extra_css' => '', + ) + parent::defaultSettings(); + } + + /** + * {@inheritdoc} + */ + public function settingsForm(array $form, FormStateInterface $form_state) { + $element = array(); + + $element['share_services'] = array( + '#title' => t('Services'), + '#type' => 'textfield', + '#size' => 80, + '#default_value' => $this->getSetting('share_services'), + '#required' => TRUE, + '#element_validate' => array($this, 'addThisDisplayElementServicesValidate'), + '#description' => + t('Specify the names of the sharing services and seperate them with a , (comma). The names on this list are valid.') . + t('Elements that are available but not ont the services list are (!services).', + array('!services' => 'bubble_style, pill_style, tweet, facebook_send, twitter_follow_native, google_plusone, stumbleupon_badge, counter_* (several supported services), linkedin_counter') + ), + ); + $element['buttons_size'] = array( + '#title' => t('Buttons size'), + '#type' => 'select', + '#default_value' => $this->getSetting('buttons_size'), + '#options' => array( + 'addthis_16x16_style' => t('Small (16x16)'), + 'addthis_32x32_style' => t('Big (32x32)'), + ), + ); + $element['counter_orientation'] = array( + '#title' => t('Counter orientation'), + '#description' => t('Specify the way service counters are oriented.'), + '#type' => 'select', + '#default_value' => $this->getSetting('counter_orientation'), + '#options' => array( + 'horizontal' => t('Horizontal'), + 'vertical' => t('Vertical'), + ) + ); + $element['extra_css'] = array( + '#title' => t('Extra CSS declaration'), + '#type' => 'textfield', + '#size' => 40, + '#default_value' => $this->getSetting('extra_css'), + '#description' => t('Specify extra CSS classes to apply to the toolbox'), + ); + + return $element; + } + + public function addThisDisplayElementServicesValidate(array $element, FormStateInterface $form_state){ + $bad = FALSE; + + $services = trim($element['#value']); + $services = str_replace(' ', '', $services); + + if (!preg_match('/^[a-z\_\,0-9]+$/', $services)) { + $bad = TRUE; + } + // @todo Validate the service names against AddThis.com. Give a notice when there are bad names. + + // Return error. + if ($bad) { + form_error($element, t('The declared services are incorrect or nonexistent.')); + } + } + + /** + * {@inheritdoc} + */ + public function viewElements(FieldItemListInterface $items) { + //@TODO Implement viewElements() + $element = array(); + $display_type = 'addthis_disabled'; + + + + $markup = array( + '#display' => array(), + ); + } + +} \ No newline at end of file diff --git a/src/Plugin/Field/FieldFormatter/AddThisFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisFormatter.php index eaa2a4d..e73d6a9 100644 --- a/src/Plugin/Field/FieldFormatter/AddThisFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AddThisFormatter.php @@ -17,7 +17,7 @@ * id = "addthis_disabled", * label = @Translation("AddThis Disabled"), * field_types = { -* "addthis_disabled" +* "addthis" * } * ) */ From 353553a021d4e8672154bec0ad4aed5afd26d51d Mon Sep 17 00:00:00 2001 From: Doyle Date: Tue, 11 Aug 2015 16:43:20 -0400 Subject: [PATCH 06/77] Implemented twig templates for addthis_wrapper and addthis_element displays. Implemented new render elements for addthis_wrapper and addthis_element. Adjusted/implemented logic for Toolbox display. --- addthis.module | 51 +------- src/AddThis.php | 15 +-- src/Element/AddThisElement.php | 57 +++++++++ src/Element/AddThisWrapper.php | 54 +++++++++ src/Plugin/Block/AddThisBlock.php | 110 +++++++++++++----- .../AddThisBasicButtonFormatter.php | 6 +- .../AddThisBasicToolboxFormatter.php | 102 ++++++++++++++-- .../Field/FieldWidget/AddThisWidget.php | 2 +- templates/addthis-element.html.twig | 12 ++ templates/addthis-wrapper.html.twig | 14 +++ 10 files changed, 329 insertions(+), 94 deletions(-) create mode 100644 src/Element/AddThisElement.php create mode 100644 src/Element/AddThisWrapper.php create mode 100644 templates/addthis-element.html.twig create mode 100644 templates/addthis-wrapper.html.twig diff --git a/addthis.module b/addthis.module index 670fb0e..dfc44e4 100644 --- a/addthis.module +++ b/addthis.module @@ -3,7 +3,7 @@ /** * Implements hook_theme(). */ -function addthis_theme($existing, $type, $theme, $path) { +function addthis_theme() { return array( 'addthis_wrapper' => array( 'render element' => 'element', @@ -11,13 +11,13 @@ function addthis_theme($existing, $type, $theme, $path) { 'addthis_element' => array( 'render element' => 'element', ), -// 'addthis' => array( -// 'render element' => 'element', -// ), ); } - +/** + * @TODO Find another way to do this since it breaks aggregation. + * @param array $page + */ function addthis_page_attachments(array &$page){ $page['#attached']['library'][] = 'addthis/addthis.global'; @@ -42,7 +42,7 @@ function addthis_library_info_alter(){ function _addthis_field_info_formatter_field_type($field_type = NULL) { $formatters = \Drupal::Service('plugin.manager.field.formatter')->getDefinitions(); foreach ($formatters as $key => $formatter) { - if (!in_array((!isset($field_type) ? 'addthis' : $field_type), $formatter['field types'])) { + if (!in_array((!isset($field_type) ? 'addthis' : $field_type), $formatter['field_types'])) { unset($formatters[$key]); } } @@ -50,42 +50,3 @@ function _addthis_field_info_formatter_field_type($field_type = NULL) { } - -function theme_addthis_wrapper($variables) { - $element = $variables['addthis_wrapper']; - $output = '<' . $element['#tag'] . drupal_attributes($element['#attributes']) . '>'; - $children = element_children($element); - - if (count($children) > 0) { - foreach ($children as $child) { - $output .= render($element[$child]); - } - } - - $output .= '\n"; - return $output; -} - -/** - * Theme the elements that are created in the AddThis module. - * - * This is created with hook_addthis_element. - */ -function theme_addthis_element($variables) { - $element = $variables['addthis_element']; - - if (!isset($element['#value'])) { - return '<' . $element['#tag'] . drupal_attributes($element['#attributes']) . " />\n"; - } - - $output = '<' . $element['#tag'] . drupal_attributes($element['#attributes']) . '>'; - if (isset($element['#value_prefix'])) { - $output .= $element['#value_prefix']; - } - $output .= $element['#value']; - if (isset($element['#value_suffix'])) { - $output .= $element['#value_suffix']; - } - $output .= '\n"; - return $output; -} \ No newline at end of file diff --git a/src/AddThis.php b/src/AddThis.php index 5edcd11..cf4f844 100644 --- a/src/AddThis.php +++ b/src/AddThis.php @@ -121,25 +121,18 @@ public function getServices() { return $rows; } - /** - * Get the type used for the block. - */ - public function getBlockDisplayType() { - $block_widget_type = $this->config->get(self::BLOCK_WIDGET_TYPE_KEY); - $block_widget_type = isset($block_widget_type) ? $block_widget_type : self::WIDGET_TYPE_DISABLED; - return $block_widget_type; - } /** * Get the settings used by the block display. */ - public function getBlockDisplaySettings() { + public function getBlockDisplaySettings($widget_type) { $block_widget_settings = $this->config->get(self::BLOCK_WIDGET_SETTINGS_KEY); $block_widget_settings = isset($block_widget_settings) ? $block_widget_settings : NULL; $settings = $block_widget_settings; - if ($settings == NULL && $this->getBlockDisplayType() != self::WIDGET_TYPE_DISABLED) { - $settings = \Drupal::service('plugin.manager.field.formatter')->getDefinition($this->getBlockDisplayType()); + if ($settings == NULL && $widget_type != self::WIDGET_TYPE_DISABLED) { + $settings = \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings($widget_type); + } return $settings; diff --git a/src/Element/AddThisElement.php b/src/Element/AddThisElement.php new file mode 100644 index 0000000..52df08c --- /dev/null +++ b/src/Element/AddThisElement.php @@ -0,0 +1,57 @@ + 'addthis_element', + '#pre_render' => [ + [$class, 'preRenderAddThisElement'], + ] + ]; + } + + + public static function preRenderAddThisElement($element){ + if (!isset($element['#value'])) { + $element['addthis_element'] = [ + '#markup' => '<' . $element['#tag'] . new Attribute($element['#attributes']) . " />\n" + ]; + return $element; + } + + $output = '<' . $element['#tag'] . new Attribute($element['#attributes']) . '>'; + if (isset($element['#value_prefix'])) { + $output .= $element['#value_prefix']; + } + $output .= $element['#value']; + if (isset($element['#value_suffix'])) { + $output .= $element['#value_suffix']; + } + $output .= '\n"; + $element['addthis_element'] = [ + '#markup' => $output, + ]; + + return $element; + } + +} \ No newline at end of file diff --git a/src/Element/AddThisWrapper.php b/src/Element/AddThisWrapper.php new file mode 100644 index 0000000..da45361 --- /dev/null +++ b/src/Element/AddThisWrapper.php @@ -0,0 +1,54 @@ + 'addthis_wrapper', + '#pre_render' => array( + array($class, 'preRenderAddThisWrapper'), + ) + ); + } + + /** + * @param $element + * @return mixed + */ + public static function preRenderAddThisWrapper($element){ + $output = '<' . $element['#tag'] . new Attribute($element['#attributes']) . '>'; + $children = Element::children($element); + +// if (count($children) > 0) { +// foreach ($children as $child) { +// $output .= render($element[$child]); +// } +// } + + $output .= '\n"; + $element['addthis_wrapper'] = [ + '#markup' => $output + ]; + + return $element; + } + +} \ No newline at end of file diff --git a/src/Plugin/Block/AddThisBlock.php b/src/Plugin/Block/AddThisBlock.php index 9160fb4..101c892 100644 --- a/src/Plugin/Block/AddThisBlock.php +++ b/src/Plugin/Block/AddThisBlock.php @@ -37,19 +37,8 @@ public function defaultConfiguration() { */ public function build() { - //@TODO Implement block markup - - $widget_type = AddThis::getInstance()->getBlockDisplayType(); - $widget_settings = AddThis::getInstance()->getBlockDisplaySettings(); - - $markup = AddThis::getInstance()->getDisplayMarkup('addthis_basic_button'); - $formatters = \Drupal::Service('plugin.manager.field.formatter')->getDefinitions(); - //$markup = 'Testing Markup'; - - - $button_img = 'http://s7.addthis.com/static/btn/sm-share-en.gif'; $element = array( - '#theme' => 'addthis_wrapper', + '#type' => 'addthis_wrapper', '#tag' => 'a', '#attributes' => array( 'class' => array( @@ -58,28 +47,93 @@ public function build() ), ); - $test = drupal_render($element); // Add the widget script. $script_manager = AddThisScriptManager::getInstance(); $script_manager->attachJsToElement($element); - // Create img button. - $image = array( - '#theme' => 'addthis_element', - '#tag' => 'img', - '#attributes' => array( - 'src' => $button_img, - 'alt' => t('Share page with AddThis'), - ), - ); - $element[] = $image; + $widget_type = $this->configuration['type']; + $widget_settings = AddThis::getInstance()->getBlockDisplaySettings($widget_type); + + $services = trim($widget_settings['share_services']); + $services = str_replace(' ', '', $services); + $services = explode(',', $services); + $items = array(); + + // All service elements + $items = array(); + foreach ($services as $service) { + $items[$service] = array( + '#type' => 'addthis_element', + '#tag' => 'a', + '#value' => '', + '#attributes' => array( + 'href' => AddThis::getInstance()->getBaseBookmarkUrl(), + 'class' => array( + 'addthis_button_' . $service, + ), + ), + '#addthis_service' => $service, + ); + + // Add individual counters. + if (strpos($service, 'counter_') === 0) { + $items[$service]['#attributes']['class'] = array("addthis_$service"); + } + + // Basic implementations of bubble counter orientation. + // @todo Figure all the bubbles out and add them. + // Still missing: tweetme, hyves and stubleupon, google_plusone_badge. + // + $orientation = ($widget_settings['counter_orientation'] == 'horizontal' ? TRUE : FALSE); + switch ($service) { + case 'linkedin_counter': + $items[$service]['#attributes'] += array( + 'li:counter' => ($orientation ? '' : 'top'), + ); + break; + case 'facebook_like': + $items[$service]['#attributes'] += array( + 'fb:like:layout' => ($orientation ? 'button_count' : 'box_count') + ); + break; + case 'facebook_share': + $items[$service]['#attributes'] += array( + 'fb:share:layout' => ($orientation ? 'button_count' : 'box_count') + ); + break; + case 'google_plusone': + $items[$service]['#attributes'] += array( + 'g:plusone:size' => ($orientation ? 'standard' : 'tall') + ); + break; + case 'tweet': + $items[$service]['#attributes'] += array( + 'tw:count' => ($orientation ? 'horizontal' : 'vertical'), + 'tw:via' => AddThis::getInstance()->getTwitterVia(), + ); + break; + case 'bubble_style': + $items[$service]['#attributes']['class'] = array( + 'addthis_counter', 'addthis_bubble_style' + ); + break; + case 'pill_style': + $items[$service]['#attributes']['class'] = array( + 'addthis_counter', 'addthis_pill_style' + ); + break; + } + } + + $element += $items; $markup = render($element); return array( - 'content' => $markup, + '#markup' => $markup ); + } function blockForm($form, FormStateInterface $form_state) { @@ -90,8 +144,8 @@ function blockForm($form, FormStateInterface $form_state) { ); // Retrieve settings. - $addthis_type = AddThis::getInstance()->getBlockDisplayType(); - $addthis_settings = AddThis::getInstance()->getBlockDisplaySettings(); + $widget_type = $this->configuration['type']; + //$addthis_settings = AddThis::getInstance()->getBlockDisplaySettings(); // The list of formatters. @@ -108,9 +162,11 @@ function blockForm($form, FormStateInterface $form_state) { return $form; } public function blockSubmit($form, FormStateInterface $form_state) { - $this->configuration['type'] = $form_state->getValue('type'); + $this->configuration['type'] = $form_state->getValue(['settings', 'addthis_settings', 'type']); } + + } ?> \ No newline at end of file diff --git a/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php index 56bf1e0..bedca81 100644 --- a/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php @@ -21,7 +21,7 @@ * } * ) */ -class AddThisBasicButtonFormatter extends FormatterBase { +class AddThisBasicButtonFormatter extends AddThisFormatter { /** * {@inheritdoc} @@ -76,7 +76,7 @@ public function viewElements(FieldItemListInterface $items) { //$extra_css = isset($settings['extra_css']) ? $settings['extra_css'] : ''; $element = array( - '#theme' => 'addthis_wrapper', + '#type' => 'addthis_wrapper', '#tag' => 'a', '#attributes' => array( 'class' => array( @@ -92,7 +92,7 @@ public function viewElements(FieldItemListInterface $items) { // Create img button. $image = array( - '#theme' => 'addthis_element', + '#type' => 'addthis_element', '#tag' => 'img', '#attributes' => array( 'src' => $button_img, diff --git a/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php index f1ec0dc..31f4715 100644 --- a/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php @@ -9,9 +9,11 @@ use Drupal\Core\Field\FormatterBase; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\addthis\AddThis; +use Drupal\addthis\Services\AddThisScriptManager; /** - * Plugin implementation of the 'addthis_disabled' formatter. + * Plugin implementation of the 'addthis_basic_toolbox' formatter. * * @FieldFormatter( * id = "addthis_basic_toolbox", @@ -21,7 +23,7 @@ * } * ) */ -class AddThisBasicToolboxFormatter extends FormatterBase { +class AddThisBasicToolboxFormatter extends AddThisFormatter { /** * {@inheritdoc} @@ -105,14 +107,100 @@ public function addThisDisplayElementServicesValidate(array $element, FormStateI * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items) { - //@TODO Implement viewElements() - $element = array(); - $display_type = 'addthis_disabled'; + $element = array( + '#type' => 'addthis_wrapper', + '#tag' => 'a', + '#attributes' => array( + 'class' => array( + 'addthis_button', + ), + ), + ); + + + // Add the widget script. + $script_manager = AddThisScriptManager::getInstance(); + $script_manager->attachJsToElement($element); + $widget_settings = $this->getSettings(); - $markup = array( - '#display' => array(), + $services = trim($widget_settings['share_services']); + $services = str_replace(' ', '', $services); + $services = explode(',', $services); + $items = array(); + + // All service elements + $items = array(); + foreach ($services as $service) { + $items[$service] = array( + '#type' => 'addthis_element', + '#tag' => 'a', + '#value' => '', + '#attributes' => array( + 'href' => AddThis::getInstance()->getBaseBookmarkUrl(), + 'class' => array( + 'addthis_button_' . $service, + ), + ), + '#addthis_service' => $service, + ); + + // Add individual counters. + if (strpos($service, 'counter_') === 0) { + $items[$service]['#attributes']['class'] = array("addthis_$service"); + } + + // Basic implementations of bubble counter orientation. + // @todo Figure all the bubbles out and add them. + // Still missing: tweetme, hyves and stubleupon, google_plusone_badge. + // + $orientation = ($widget_settings['counter_orientation'] == 'horizontal' ? TRUE : FALSE); + switch ($service) { + case 'linkedin_counter': + $items[$service]['#attributes'] += array( + 'li:counter' => ($orientation ? '' : 'top'), + ); + break; + case 'facebook_like': + $items[$service]['#attributes'] += array( + 'fb:like:layout' => ($orientation ? 'button_count' : 'box_count') + ); + break; + case 'facebook_share': + $items[$service]['#attributes'] += array( + 'fb:share:layout' => ($orientation ? 'button_count' : 'box_count') + ); + break; + case 'google_plusone': + $items[$service]['#attributes'] += array( + 'g:plusone:size' => ($orientation ? 'standard' : 'tall') + ); + break; + case 'tweet': + $items[$service]['#attributes'] += array( + 'tw:count' => ($orientation ? 'horizontal' : 'vertical'), + 'tw:via' => AddThis::getInstance()->getTwitterVia(), + ); + break; + case 'bubble_style': + $items[$service]['#attributes']['class'] = array( + 'addthis_counter', 'addthis_bubble_style' + ); + break; + case 'pill_style': + $items[$service]['#attributes']['class'] = array( + 'addthis_counter', 'addthis_pill_style' + ); + break; + } + } + + $element += $items; + + $markup = render($element); + return array( + '#markup' => $markup ); } diff --git a/src/Plugin/Field/FieldWidget/AddThisWidget.php b/src/Plugin/Field/FieldWidget/AddThisWidget.php index 2f4c6f7..7041f29 100644 --- a/src/Plugin/Field/FieldWidget/AddThisWidget.php +++ b/src/Plugin/Field/FieldWidget/AddThisWidget.php @@ -18,7 +18,7 @@ * id = "addthis_button_widget", * label = @Translation("AddThis button"), * field_types = { - * "disabled" + * "addthis" * }, * multiple_values = FALSE * ) diff --git a/templates/addthis-element.html.twig b/templates/addthis-element.html.twig new file mode 100644 index 0000000..e25fd75 --- /dev/null +++ b/templates/addthis-element.html.twig @@ -0,0 +1,12 @@ +{# +/** + * @file + * Default theme implementation to display a node.. + * Available variables: + * - element: AddThis Wrapper to be rendered + * - element['']: Something else to render. + * + */ +#} + +{{ element }} \ No newline at end of file diff --git a/templates/addthis-wrapper.html.twig b/templates/addthis-wrapper.html.twig new file mode 100644 index 0000000..2df32ef --- /dev/null +++ b/templates/addthis-wrapper.html.twig @@ -0,0 +1,14 @@ +{# +/** + * @file + * Default theme implementation to display a node. + * Available variables: + * - element: AddThis Wrapper to be rendered + * - element['']: Something else to render. + * + */ +#} + +
+{{ element }} +
\ No newline at end of file From 62012931f01b3aabeacc0edf19d26405e9bea62e Mon Sep 17 00:00:00 2001 From: Doyle Date: Tue, 11 Aug 2015 17:14:00 -0400 Subject: [PATCH 07/77] Added missing classes from the AddThisBasicButton setup. --- addthis.links.menu.yml | 2 +- src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/addthis.links.menu.yml b/addthis.links.menu.yml index 9e001f8..af1781e 100644 --- a/addthis.links.menu.yml +++ b/addthis.links.menu.yml @@ -3,7 +3,7 @@ addthis.settings: description: 'Manage your site''s AddThis settings.' parent: system.admin_config_ui route_name: addthis.settings -addthis.settings: +addthis.settings.advanced: title: AddThis description: 'Manage your site''s Advanced AddThis settings.' parent: addthis.settings diff --git a/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php index bedca81..3d1170b 100644 --- a/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php @@ -9,6 +9,8 @@ use Drupal\Core\Field\FormatterBase; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\addthis\AddThis; +use Drupal\addthis\Services\AddThisScriptManager; /** * Plugin implementation of the 'addthis_basic_button' formatter. From 9777ae52da0c8dee28f5dffd7a307c7b7542ac8f Mon Sep 17 00:00:00 2001 From: Doyle Date: Wed, 12 Aug 2015 13:24:35 -0400 Subject: [PATCH 08/77] Abstracted out blocks and field formatters into their own modules. Added configuration settings to block to match AddThisBasicToolbox for more flexibility. --- addthis.module | 4 +- addthis_block/addthis_block.info.yml | 11 +++ .../src}/Plugin/Block/AddThisBlock.php | 74 +++++++++++++------ addthis_fields/addthis_fields.info.yml | 11 +++ .../AddThisBasicButtonFormatter.php | 7 +- .../AddThisBasicToolboxFormatter.php | 7 +- js/addthis.js | 2 +- src/AddThis.php | 32 -------- src/Element/AddThisElement.php | 6 ++ src/Element/AddThisWrapper.php | 10 +-- templates/addthis-wrapper.html.twig | 4 +- 11 files changed, 92 insertions(+), 76 deletions(-) create mode 100644 addthis_block/addthis_block.info.yml rename {src => addthis_block/src}/Plugin/Block/AddThisBlock.php (56%) create mode 100644 addthis_fields/addthis_fields.info.yml rename {src => addthis_fields/src}/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php (92%) rename {src => addthis_fields/src}/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php (96%) diff --git a/addthis.module b/addthis.module index dfc44e4..5523363 100644 --- a/addthis.module +++ b/addthis.module @@ -24,8 +24,8 @@ function addthis_page_attachments(array &$page){ // Include the widget js when we include on all but admin pages. //if (!path_is_admin(current_path()) && AddThis::getInstance()->getWidgetJsInclude() == AddThis::WIDGET_JS_INCLUDE_PAGE) { - $manager = \Drupal\addthis\Services\AddThisScriptManager::getInstance(); - $manager->attachJsToElement($page); + //$manager = \Drupal\addthis\Services\AddThisScriptManager::getInstance(); + //$manager->attachJsToElement($page); //} } diff --git a/addthis_block/addthis_block.info.yml b/addthis_block/addthis_block.info.yml new file mode 100644 index 0000000..6c2d411 --- /dev/null +++ b/addthis_block/addthis_block.info.yml @@ -0,0 +1,11 @@ +name: AddThis Block +description: Provides a configurable block to render AddThis elements. +type: module +package: Sharing +core: 8.x + +dependencies: + - addthis + + + diff --git a/src/Plugin/Block/AddThisBlock.php b/addthis_block/src/Plugin/Block/AddThisBlock.php similarity index 56% rename from src/Plugin/Block/AddThisBlock.php rename to addthis_block/src/Plugin/Block/AddThisBlock.php index 101c892..a19dea0 100644 --- a/src/Plugin/Block/AddThisBlock.php +++ b/addthis_block/src/Plugin/Block/AddThisBlock.php @@ -1,10 +1,10 @@ 'addthis_disabled', + 'share_services' => 'facebook,twitter', + 'buttons_size' => 'addthis_16x16_style', + 'counter_orientation' => 'horizontal', + 'extra_css' => '', ); } @@ -46,17 +49,14 @@ public function build() ), ), ); - - // Add the widget script. $script_manager = AddThisScriptManager::getInstance(); $script_manager->attachJsToElement($element); - $widget_type = $this->configuration['type']; - $widget_settings = AddThis::getInstance()->getBlockDisplaySettings($widget_type); + $config = $this->configuration; - $services = trim($widget_settings['share_services']); + $services = trim($config['share_services']); $services = str_replace(' ', '', $services); $services = explode(',', $services); $items = array(); @@ -86,7 +86,7 @@ public function build() // @todo Figure all the bubbles out and add them. // Still missing: tweetme, hyves and stubleupon, google_plusone_badge. // - $orientation = ($widget_settings['counter_orientation'] == 'horizontal' ? TRUE : FALSE); + $orientation = ($config['counter_orientation'] == 'horizontal' ? TRUE : FALSE); switch ($service) { case 'linkedin_counter': $items[$service]['#attributes'] += array( @@ -143,26 +143,54 @@ function blockForm($form, FormStateInterface $form_state) { '#title' => 'Display settings', ); - // Retrieve settings. - $widget_type = $this->configuration['type']; - //$addthis_settings = AddThis::getInstance()->getBlockDisplaySettings(); - - // The list of formatters. - $formatter_options = AddThis::getInstance()->getDisplayTypes(); - - $form['settings']['addthis_settings']['type'] = array( + $form['settings']['addthis_settings']['share_services'] = array( + '#title' => t('Services'), + '#type' => 'textfield', + '#size' => 80, + '#default_value' => $this->configuration['share_services'], + '#required' => TRUE, + '#element_validate' => array($this, 'addThisDisplayElementServicesValidate'), + '#description' => + t('Specify the names of the sharing services and seperate them with a , (comma). The names on this list are valid.') . + t('Elements that are available but not ont the services list are (!services).', + array('!services' => 'bubble_style, pill_style, tweet, facebook_send, twitter_follow_native, google_plusone, stumbleupon_badge, counter_* (several supported services), linkedin_counter') + ), + ); + $form['settings']['addthis_settings']['buttons_size'] = array( + '#title' => t('Buttons size'), '#type' => 'select', - '#title' => t('Formatter for @title', array('@title' => 'AddThis block')), - '#title_display' => 'invisible', - '#options' => $formatter_options, - '#default_value' => $this->configuration['type'], - '#attributes' => array('class' => array('addthis-display-type')), + '#default_value' => $this->configuration['buttons_size'], + '#options' => array( + 'addthis_16x16_style' => t('Small (16x16)'), + 'addthis_32x32_style' => t('Big (32x32)'), + ), + ); + $form['settings']['addthis_settings']['counter_orientation'] = array( + '#title' => t('Counter orientation'), + '#description' => t('Specify the way service counters are oriented.'), + '#type' => 'select', + '#default_value' => $this->configuration['counter_orientation'], + '#options' => array( + 'horizontal' => t('Horizontal'), + 'vertical' => t('Vertical'), + ) + ); + $form['settings']['addthis_settings']['extra_css'] = array( + '#title' => t('Extra CSS declaration'), + '#type' => 'textfield', + '#size' => 40, + '#default_value' => $this->configuration['extra_css'], + '#description' => t('Specify extra CSS classes to apply to the toolbox'), ); return $form; } public function blockSubmit($form, FormStateInterface $form_state) { - $this->configuration['type'] = $form_state->getValue(['settings', 'addthis_settings', 'type']); + $this->configuration['share_services'] = $form_state->getValue(['settings', 'addthis_settings', 'share_services']); + $this->configuration['buttons_size'] = $form_state->getValue(['settings', 'addthis_settings', 'buttons_size']); + $this->configuration['counter_orientation'] = $form_state->getValue(['settings', 'addthis_settings', 'counter_orientation']); + $this->configuration['extra_css'] = $form_state->getValue(['settings', 'addthis_settings', 'extra_css']); + } diff --git a/addthis_fields/addthis_fields.info.yml b/addthis_fields/addthis_fields.info.yml new file mode 100644 index 0000000..f50f934 --- /dev/null +++ b/addthis_fields/addthis_fields.info.yml @@ -0,0 +1,11 @@ +name: AddThis Fields +description: Provides a basic field definitions for rendering AddThis. +type: module +package: Sharing +core: 8.x + +dependencies: + - addthis + + + diff --git a/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php similarity index 92% rename from src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php rename to addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php index 3d1170b..f77b9dd 100644 --- a/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php +++ b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php @@ -1,10 +1,10 @@ attachJsToElement($element); - $widget_settings = $this->getSettings(); $services = trim($widget_settings['share_services']); diff --git a/js/addthis.js b/js/addthis.js index 6861a89..b845355 100644 --- a/js/addthis.js +++ b/js/addthis.js @@ -6,7 +6,7 @@ (function ($, Drupal, drupalSettings) { Drupal.behaviors.addthis = { scriptLoaded: false, - attach: function(context) { + attach: function(context, settings) { // The addthis configuration is not loaded but the settings are passed // to do so. diff --git a/src/AddThis.php b/src/AddThis.php index cf4f844..18e6dba 100644 --- a/src/AddThis.php +++ b/src/AddThis.php @@ -243,38 +243,6 @@ public function getDisplayTypes() { return $displays; } - /* - * Get markup for a given display type. - * - * When $options does not contain #entity, link to the current URL. - * When $options does not contain #display, use default settings. - */ - public function getDisplayMarkup($display, $options = array()) { - $formatters = \Drupal::Service('plugin.manager.field.formatter')->getDefinitions(); - - if (!array_key_exists($display, $formatters)) { - return array(); - } - // The display type exists. Now get it and get the markup. - $display_information = $formatters[$display]; - - // Theme function might only give a display name and - // render on default implementation. - if (!isset($options['#display']) || - (isset($options['#display']['type']) && $options['#display']['type'] != $display)) { - $options['#display'] = isset($options['#display']) ? $options['#display'] : array(); - $options['#display'] = array_merge($options['#display'], $display_information); - $options['#display']['type'] = $display; - - } - - - $markup = array( - '#display' => $options['#display'], - ); - - return $markup; - } } \ No newline at end of file diff --git a/src/Element/AddThisElement.php b/src/Element/AddThisElement.php index 52df08c..bde22f0 100644 --- a/src/Element/AddThisElement.php +++ b/src/Element/AddThisElement.php @@ -30,6 +30,12 @@ public function getInfo() { } + /** + * Implements preRenderAddThisElement() + * - Defines consistent markup for new render type of addthis_element. + * @param $element + * @return mixed + */ public static function preRenderAddThisElement($element){ if (!isset($element['#value'])) { $element['addthis_element'] = [ diff --git a/src/Element/AddThisWrapper.php b/src/Element/AddThisWrapper.php index da45361..123799e 100644 --- a/src/Element/AddThisWrapper.php +++ b/src/Element/AddThisWrapper.php @@ -30,19 +30,13 @@ public function getInfo() { } /** + * Implements preRenderAddThisWrapper() + * - Defines consistent markup for the addthis_wrapper render element. * @param $element * @return mixed */ public static function preRenderAddThisWrapper($element){ $output = '<' . $element['#tag'] . new Attribute($element['#attributes']) . '>'; - $children = Element::children($element); - -// if (count($children) > 0) { -// foreach ($children as $child) { -// $output .= render($element[$child]); -// } -// } - $output .= '\n"; $element['addthis_wrapper'] = [ '#markup' => $output diff --git a/templates/addthis-wrapper.html.twig b/templates/addthis-wrapper.html.twig index 2df32ef..79b4857 100644 --- a/templates/addthis-wrapper.html.twig +++ b/templates/addthis-wrapper.html.twig @@ -9,6 +9,4 @@ */ #} -
-{{ element }} -
\ No newline at end of file +{{ element }} \ No newline at end of file From 5f980c61c09373214d3e6727e0fc3c371ce62b02 Mon Sep 17 00:00:00 2001 From: Doyle Date: Wed, 12 Aug 2015 13:37:28 -0400 Subject: [PATCH 09/77] Removed commented code --- addthis.module | 8 -------- 1 file changed, 8 deletions(-) diff --git a/addthis.module b/addthis.module index 5523363..9115772 100644 --- a/addthis.module +++ b/addthis.module @@ -20,14 +20,6 @@ function addthis_theme() { */ function addthis_page_attachments(array &$page){ $page['#attached']['library'][] = 'addthis/addthis.global'; - - -// Include the widget js when we include on all but admin pages. - //if (!path_is_admin(current_path()) && AddThis::getInstance()->getWidgetJsInclude() == AddThis::WIDGET_JS_INCLUDE_PAGE) { - //$manager = \Drupal\addthis\Services\AddThisScriptManager::getInstance(); - //$manager->attachJsToElement($page); - //} - } From 4d5cbfe360a602bafb6feda022ad4b7c4762c75a Mon Sep 17 00:00:00 2001 From: Doyle Date: Wed, 12 Aug 2015 13:48:15 -0400 Subject: [PATCH 10/77] Added information about included modules into README file --- README.txt | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/README.txt b/README.txt index 709d22c..cc89ab1 100644 --- a/README.txt +++ b/README.txt @@ -30,6 +30,14 @@ information. For link sharing statistics registration at http://addthis.com/ is required, but the module will work even without registration. +-- INCLUDED MODULES -- +addthis - Provides the base API to integrate with AddThis. Also creates RenderElements, +base Twig templates and global admin settings for AddThis. Does not provide any +rendering functionality on its own. +addthis_block - Provides a configurable block for displaying AddThis. +addthis_fields - Provides two field formatters for AddThis to allow for rendering on +entities. + -- CONFIGURATION -- Use the admin configuration page to configure settings and see http://drupal.org/node/1309922 @@ -42,7 +50,10 @@ and altering configuration on rendering. -- CONTACT -- -Current maintainers: +Current D8 contributors +* John Doyle (doylejd) - https://www.drupal.org/u/doylejd + +Current D7 maintainers: * Vesa Palmu (wesku) - http://drupal.org/user/75070 * Jani Palsamäki (janip) - http://drupal.org/user/1356218 * Matthias Glastra (matglas86) - http://drupal.org/user/573464 From 0349bc9e0a9b6695fe3447201c22bb08c354f202 Mon Sep 17 00:00:00 2001 From: Doyle Date: Wed, 12 Aug 2015 13:52:58 -0400 Subject: [PATCH 11/77] Added information about included modules into README file --- README.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.txt b/README.txt index cc89ab1..b366bc0 100644 --- a/README.txt +++ b/README.txt @@ -31,11 +31,11 @@ For link sharing statistics registration at http://addthis.com/ is required, but the module will work even without registration. -- INCLUDED MODULES -- -addthis - Provides the base API to integrate with AddThis. Also creates RenderElements, +**addthis** - Provides the base API to integrate with AddThis. Also creates RenderElements, base Twig templates and global admin settings for AddThis. Does not provide any rendering functionality on its own. -addthis_block - Provides a configurable block for displaying AddThis. -addthis_fields - Provides two field formatters for AddThis to allow for rendering on +**addthis_block** - Provides a configurable block for displaying AddThis. +**addthis_fields** - Provides two field formatters for AddThis to allow for rendering on entities. -- CONFIGURATION -- From c60aba24be9acefc8bb6af5b0108e80e04e52b07 Mon Sep 17 00:00:00 2001 From: Doyle Date: Wed, 12 Aug 2015 13:54:16 -0400 Subject: [PATCH 12/77] Made modules in README an ordered list --- README.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.txt b/README.txt index b366bc0..20a9390 100644 --- a/README.txt +++ b/README.txt @@ -31,11 +31,11 @@ For link sharing statistics registration at http://addthis.com/ is required, but the module will work even without registration. -- INCLUDED MODULES -- -**addthis** - Provides the base API to integrate with AddThis. Also creates RenderElements, +1. **addthis** - Provides the base API to integrate with AddThis. Also creates RenderElements, base Twig templates and global admin settings for AddThis. Does not provide any rendering functionality on its own. -**addthis_block** - Provides a configurable block for displaying AddThis. -**addthis_fields** - Provides two field formatters for AddThis to allow for rendering on +2. **addthis_block** - Provides a configurable block for displaying AddThis. +3. **addthis_fields** - Provides two field formatters for AddThis to allow for rendering on entities. -- CONFIGURATION -- From 976ac273cdd0c298468109311f0e9060aac871d4 Mon Sep 17 00:00:00 2001 From: Doyle Date: Wed, 12 Aug 2015 14:01:58 -0400 Subject: [PATCH 13/77] Removed deprecated addthis_displays module --- addthis_displays/addthis_displays.info.yml | 11 -- addthis_displays/addthis_displays.module | 156 --------------------- 2 files changed, 167 deletions(-) delete mode 100644 addthis_displays/addthis_displays.info.yml delete mode 100644 addthis_displays/addthis_displays.module diff --git a/addthis_displays/addthis_displays.info.yml b/addthis_displays/addthis_displays.info.yml deleted file mode 100644 index 87ffbe9..0000000 --- a/addthis_displays/addthis_displays.info.yml +++ /dev/null @@ -1,11 +0,0 @@ -name: AddThis Displays -description: Adds the AddThis display types to render several basic types of AddThis buttons. -type: module -core: 8.x -package: Sharing - -dependencies: - - addthis - - - diff --git a/addthis_displays/addthis_displays.module b/addthis_displays/addthis_displays.module deleted file mode 100644 index b64a15b..0000000 --- a/addthis_displays/addthis_displays.module +++ /dev/null @@ -1,156 +0,0 @@ -. - '#theme' => 'addthis_wrapper', - '#tag' => 'div', - '#attributes' => array( - 'class' => array( - 'addthis_toolbox', - 'addthis_default_style', - ($options['#display']['settings']['buttons_size'] == AddThis::CSS_32x32 ? AddThis::CSS_32x32 : NULL), - $options['#display']['settings']['extra_css'], - ), - ), - ); - $element['#attributes'] += $addthis->getAddThisAttributesMarkup($options); - - // Add the widget script. - $script_manager = AddThisScriptManager::getInstance(); - $script_manager->attachJsToElement($element); - - $services = trim($options['#display']['settings']['share_services']); - $services = str_replace(' ', '', $services); - $services = explode(',', $services); - - // All service elements - $items = array(); - foreach ($services as $service) { - $items[$service] = array( - '#theme' => 'addthis_element', - '#tag' => 'a', - '#value' => '', - '#attributes' => array( - 'href' => $addthis->getBaseBookmarkUrl(), - 'class' => array( - 'addthis_button_' . $service, - ), - ), - '#addthis_service' => $service, - ); - - // Add individual counters. - if (strpos($service, 'counter_') === 0) { - $items[$service]['#attributes']['class'] = array("addthis_$service"); - } - - // Basic implementations of bubble counter orientation. - // @todo Figure all the bubbles out and add them. - // Still missing: tweetme, hyves and stubleupon, google_plusone_badge. - // - $orientation = ($options['#display']['settings']['counter_orientation'] == 'horizontal' ? TRUE : FALSE); - switch ($service) { - case 'linkedin_counter': - $items[$service]['#attributes'] += array( - 'li:counter' => ($orientation ? '' : 'top'), - ); - break; - case 'facebook_like': - $items[$service]['#attributes'] += array( - 'fb:like:layout' => ($orientation ? 'button_count' : 'box_count') - ); - break; - case 'facebook_share': - $items[$service]['#attributes'] += array( - 'fb:share:layout' => ($orientation ? 'button_count' : 'box_count') - ); - break; - case 'google_plusone': - $items[$service]['#attributes'] += array( - 'g:plusone:size' => ($orientation ? 'standard' : 'tall') - ); - break; - case 'tweet': - $items[$service]['#attributes'] += array( - 'tw:count' => ($orientation ? 'horizontal' : 'vertical'), - 'tw:via' => AddThis::getInstance()->getTwitterVia(), - ); - break; - case 'bubble_style': - $items[$service]['#attributes']['class'] = array( - 'addthis_counter', 'addthis_bubble_style' - ); - break; - case 'pill_style': - $items[$service]['#attributes']['class'] = array( - 'addthis_counter', 'addthis_pill_style' - ); - break; - } - } - $element += $items; - - return $element; -} - -/** - * Implements hook_addthis_display_markup__[display](). - */ -function addthis_displays_addthis_display_markup__addthis_basic_button($options = array()) { - - $addthis = AddThis::getInstance(); - $settings = $options['#display']['settings']; - - $button_img = 'http://s7.addthis.com/static/btn/sm-share-en.gif'; - if (isset($settings['buttons_size']) && $settings['buttons_size'] == 'big') { - $button_img = 'http://s7.addthis.com/static/btn/v2/lg-share-en.gif'; - } - $button_img = $addthis->transformToSecureUrl($button_img); - - $extra_css = isset($settings['extra_css']) ? $settings['extra_css'] : ''; - $element = array( - '#theme' => 'addthis_wrapper', - '#tag' => 'a', - '#attributes' => array( - 'class' => array( - 'addthis_button', - $extra_css, - ), - ), - ); - $element['#attributes'] += $addthis->getAddThisAttributesMarkup($options); - - // Add the widget script. - $script_manager = AddThisScriptManager::getInstance(); - $script_manager->attachJsToElement($element); - - // Create img button. - $image = array( - '#theme' => 'addthis_element', - '#tag' => 'img', - '#attributes' => array( - 'src' => $button_img, - 'alt' => t('Share page with AddThis'), - ), - ); - $element[] = $image; - - return $element; -} From 39ab87d5b88090a9f5a3c3918eb5f51cc43f8d06 Mon Sep 17 00:00:00 2001 From: Michael Sherron Date: Wed, 12 Aug 2015 16:35:23 -0700 Subject: [PATCH 14/77] added administer advanced addthis settings permission and updated addthis router file to use new permissions --- addthis.permissions.yml | 5 ++++- addthis.routing.yml | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/addthis.permissions.yml b/addthis.permissions.yml index 71b5863..dbd85af 100644 --- a/addthis.permissions.yml +++ b/addthis.permissions.yml @@ -1,3 +1,6 @@ -administer advanced addthis: +administer addthis settings: + title: 'Administer AddThis Settings' + description: 'Perform configuration tasks for AddThis.' +administer advanced addthis settings: title: 'Administer Advanced AddThis Settings' description: 'Perform advanced configuration tasks for AddThis.' \ No newline at end of file diff --git a/addthis.routing.yml b/addthis.routing.yml index 3c87c80..825b5db 100644 --- a/addthis.routing.yml +++ b/addthis.routing.yml @@ -4,11 +4,11 @@ addthis.settings: _form: '\Drupal\addthis\Form\AddThisSettingsForm' _title: 'AddThis' requirements: - _permission: 'administer site configuration' + _permission: 'administer addthis settings' addthis.settings.advanced: path: '/admin/config/user-interface/addthis/advanced' defaults: _form: '\Drupal\addthis\Form\AddThisSettingsAdvancedForm' _title: 'AddThis Advanced Settings' requirements: - _permission: 'administer site configuration' \ No newline at end of file + _permission: 'administer advanced addthis settings' \ No newline at end of file From 17fae772f784ac054735461dff0baec2ba824f37 Mon Sep 17 00:00:00 2001 From: Michael Sherron Date: Wed, 12 Aug 2015 17:09:35 -0700 Subject: [PATCH 15/77] Creating local task file for tabbed navigation of config menus, also fixing broken advanced menu link and enforcing weight --- addthis.links.menu.yml | 12 +++++++----- addthis.links.task.yml | 8 ++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 addthis.links.task.yml diff --git a/addthis.links.menu.yml b/addthis.links.menu.yml index af1781e..86a6d3a 100644 --- a/addthis.links.menu.yml +++ b/addthis.links.menu.yml @@ -1,10 +1,12 @@ addthis.settings: - title: AddThis + title: AddThis - Basic Settings description: 'Manage your site''s AddThis settings.' parent: system.admin_config_ui route_name: addthis.settings + weight: 1 addthis.settings.advanced: - title: AddThis - description: 'Manage your site''s Advanced AddThis settings.' - parent: addthis.settings - route_name: addthis.settings.advanced \ No newline at end of file + title: AddThis - Advanced Settings + description: 'Manage your site''s advanced AddThis settings.' + parent: system.admin_config_ui + route_name: addthis.settings.advanced + weight: 2 \ No newline at end of file diff --git a/addthis.links.task.yml b/addthis.links.task.yml new file mode 100644 index 0000000..bd96cb5 --- /dev/null +++ b/addthis.links.task.yml @@ -0,0 +1,8 @@ +addthis.settings: + title: 'Basic' + route_name: addthis.settings + base_route: addthis.settings +addthis.settings.advanced: + title: 'Advanced' + route_name: addthis.settings.advanced + base_route: addthis.settings \ No newline at end of file From 0418a67f9c0188ba52cac7929b4aba9d6c38d4c0 Mon Sep 17 00:00:00 2001 From: Michael Sherron Date: Wed, 12 Aug 2015 17:16:20 -0700 Subject: [PATCH 16/77] Revert "Creating local task file for tabbed navigation of config menus, also fixing broken advanced menu link and enforcing weight" This reverts commit 17fae772f784ac054735461dff0baec2ba824f37. --- addthis.links.menu.yml | 12 +++++------- addthis.links.task.yml | 8 -------- 2 files changed, 5 insertions(+), 15 deletions(-) delete mode 100644 addthis.links.task.yml diff --git a/addthis.links.menu.yml b/addthis.links.menu.yml index 86a6d3a..af1781e 100644 --- a/addthis.links.menu.yml +++ b/addthis.links.menu.yml @@ -1,12 +1,10 @@ addthis.settings: - title: AddThis - Basic Settings + title: AddThis description: 'Manage your site''s AddThis settings.' parent: system.admin_config_ui route_name: addthis.settings - weight: 1 addthis.settings.advanced: - title: AddThis - Advanced Settings - description: 'Manage your site''s advanced AddThis settings.' - parent: system.admin_config_ui - route_name: addthis.settings.advanced - weight: 2 \ No newline at end of file + title: AddThis + description: 'Manage your site''s Advanced AddThis settings.' + parent: addthis.settings + route_name: addthis.settings.advanced \ No newline at end of file diff --git a/addthis.links.task.yml b/addthis.links.task.yml deleted file mode 100644 index bd96cb5..0000000 --- a/addthis.links.task.yml +++ /dev/null @@ -1,8 +0,0 @@ -addthis.settings: - title: 'Basic' - route_name: addthis.settings - base_route: addthis.settings -addthis.settings.advanced: - title: 'Advanced' - route_name: addthis.settings.advanced - base_route: addthis.settings \ No newline at end of file From 1cc145eaa32bbee79e1e9eb093bd01d9c568961f Mon Sep 17 00:00:00 2001 From: Michael Sherron Date: Wed, 12 Aug 2015 17:23:00 -0700 Subject: [PATCH 17/77] Creating local task file for tabbed navigation of config menus, also fixing broken advanced menu link and enforcing weight. --- addthis.links.menu.yml | 12 +++++++----- addthis.links.task.yml | 8 ++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 addthis.links.task.yml diff --git a/addthis.links.menu.yml b/addthis.links.menu.yml index af1781e..86a6d3a 100644 --- a/addthis.links.menu.yml +++ b/addthis.links.menu.yml @@ -1,10 +1,12 @@ addthis.settings: - title: AddThis + title: AddThis - Basic Settings description: 'Manage your site''s AddThis settings.' parent: system.admin_config_ui route_name: addthis.settings + weight: 1 addthis.settings.advanced: - title: AddThis - description: 'Manage your site''s Advanced AddThis settings.' - parent: addthis.settings - route_name: addthis.settings.advanced \ No newline at end of file + title: AddThis - Advanced Settings + description: 'Manage your site''s advanced AddThis settings.' + parent: system.admin_config_ui + route_name: addthis.settings.advanced + weight: 2 \ No newline at end of file diff --git a/addthis.links.task.yml b/addthis.links.task.yml new file mode 100644 index 0000000..bd96cb5 --- /dev/null +++ b/addthis.links.task.yml @@ -0,0 +1,8 @@ +addthis.settings: + title: 'Basic' + route_name: addthis.settings + base_route: addthis.settings +addthis.settings.advanced: + title: 'Advanced' + route_name: addthis.settings.advanced + base_route: addthis.settings \ No newline at end of file From 175cb1b39f1d170ae07897c35ed0d4971bc23c5a Mon Sep 17 00:00:00 2001 From: Doyle Date: Thu, 13 Aug 2015 11:15:03 -0400 Subject: [PATCH 18/77] Resolved issue with icons display on admin page --- src/AddThis.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/AddThis.php b/src/AddThis.php index 18e6dba..e64b0d8 100644 --- a/src/AddThis.php +++ b/src/AddThis.php @@ -115,7 +115,12 @@ public function getServices() { foreach ($services['data'] as $service) { $serviceCode = SafeMarkup::checkPlain($service['code']); $serviceName = SafeMarkup::checkPlain($service['name']); - $rows[$serviceCode] = ' ' . $serviceName; + $service = array( + '#type' => 'inline_template', + '#template' => ' ' . $serviceName, + ); + //$rows[$serviceCode] = ' ' . $serviceName; + $rows[$serviceCode] = $service; } } return $rows; From 3489a2934a8c946ec9cfcff9c0f6c58b9244d3dd Mon Sep 17 00:00:00 2001 From: Jason Enter Date: Thu, 13 Aug 2015 12:33:07 -0400 Subject: [PATCH 19/77] Fix rendering of AddThisToolbox. #17 --- addthis.libraries.yml | 4 +--- addthis_block/src/Plugin/Block/AddThisBlock.php | 13 ++++++++----- .../FieldFormatter/AddThisBasicToolboxFormatter.php | 13 ++++++++----- src/Element/AddThisWrapper.php | 8 +++++++- templates/addthis-element.html.twig | 2 +- templates/addthis-wrapper.html.twig | 2 +- 6 files changed, 26 insertions(+), 16 deletions(-) diff --git a/addthis.libraries.yml b/addthis.libraries.yml index 6adb29c..fa93d09 100644 --- a/addthis.libraries.yml +++ b/addthis.libraries.yml @@ -17,6 +17,4 @@ addthis.global: addthis.widget: version: VERSION js: - http://s7.addthis.com/js/300/addthis_widget.js: { type: external, scope: footer } - - + http://s7.addthis.com/js/300/addthis_widget.js: { type: external, scope: footer } \ No newline at end of file diff --git a/addthis_block/src/Plugin/Block/AddThisBlock.php b/addthis_block/src/Plugin/Block/AddThisBlock.php index a19dea0..6417763 100644 --- a/addthis_block/src/Plugin/Block/AddThisBlock.php +++ b/addthis_block/src/Plugin/Block/AddThisBlock.php @@ -40,22 +40,24 @@ public function defaultConfiguration() { */ public function build() { + $config = $this->configuration; $element = array( '#type' => 'addthis_wrapper', - '#tag' => 'a', + '#tag' => 'div', '#attributes' => array( 'class' => array( - 'addthis_button', + 'addthis_toolbox', + 'addthis_default_style', + ($config['buttons_size'] == AddThis::CSS_32x32 ? AddThis::CSS_32x32 : NULL), + $config['extra_css'], ), ), ); + // Add the widget script. $script_manager = AddThisScriptManager::getInstance(); $script_manager->attachJsToElement($element); - - $config = $this->configuration; - $services = trim($config['share_services']); $services = str_replace(' ', '', $services); $services = explode(',', $services); @@ -130,6 +132,7 @@ public function build() $element += $items; $markup = render($element); + return array( '#markup' => $markup ); diff --git a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php index 2950212..9d4bd50 100644 --- a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php +++ b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php @@ -107,23 +107,26 @@ public function addThisDisplayElementServicesValidate(array $element, FormStateI * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items) { + + $widget_settings = $this->getSettings(); + $element = array( '#type' => 'addthis_wrapper', - '#tag' => 'a', + '#tag' => 'div', '#attributes' => array( 'class' => array( - 'addthis_button', + 'addthis_toolbox', + 'addthis_default_style', + ($widget_settings['buttons_size'] == AddThis::CSS_32x32 ? AddThis::CSS_32x32 : NULL), + $widget_settings['extra_css'], ), ), ); - // Add the widget script. $script_manager = AddThisScriptManager::getInstance(); $script_manager->attachJsToElement($element); - $widget_settings = $this->getSettings(); - $services = trim($widget_settings['share_services']); $services = str_replace(' ', '', $services); $services = explode(',', $services); diff --git a/src/Element/AddThisWrapper.php b/src/Element/AddThisWrapper.php index 123799e..3dbb8fb 100644 --- a/src/Element/AddThisWrapper.php +++ b/src/Element/AddThisWrapper.php @@ -37,7 +37,13 @@ public function getInfo() { */ public static function preRenderAddThisWrapper($element){ $output = '<' . $element['#tag'] . new Attribute($element['#attributes']) . '>'; - $output .= '\n"; + $children = Element::children($element); + if (count($children) > 0) { + foreach ($children as $child) { + $output .= render($element[$child]); + } + } + $output .= ' \n"; $element['addthis_wrapper'] = [ '#markup' => $output ]; diff --git a/templates/addthis-element.html.twig b/templates/addthis-element.html.twig index e25fd75..0abe9d9 100644 --- a/templates/addthis-element.html.twig +++ b/templates/addthis-element.html.twig @@ -9,4 +9,4 @@ */ #} -{{ element }} \ No newline at end of file +{{ element.addthis_element }} \ No newline at end of file diff --git a/templates/addthis-wrapper.html.twig b/templates/addthis-wrapper.html.twig index 79b4857..2732157 100644 --- a/templates/addthis-wrapper.html.twig +++ b/templates/addthis-wrapper.html.twig @@ -9,4 +9,4 @@ */ #} -{{ element }} \ No newline at end of file +{{ element.addthis_wrapper }} \ No newline at end of file From d363971b3f6fa7c8c960e4b72a81932f4fbf3ce0 Mon Sep 17 00:00:00 2001 From: Jason Enter Date: Thu, 13 Aug 2015 13:32:58 -0400 Subject: [PATCH 20/77] inline block for admin icons --- css/addthis.admin.css | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/css/addthis.admin.css b/css/addthis.admin.css index 99cd919..f7a7bce 100644 --- a/css/addthis.admin.css +++ b/css/addthis.admin.css @@ -2,10 +2,19 @@ display: inline-block; padding-left: 20px; } + #edit-addthis-enabled-services .form-item, #edit-addthis-excluded-services .form-item { float: left; width: 20%; } + #edit-addthis-enabled-services .form-item .description { clear: both; } + +#edit-addthis-excluded-services label.option, +#edit-addthis-enabled-services label.option, +#edit-addthis-excluded-services .addthis_service_icon, +#edit-addthis-enabled-services .addthis_service_icon { + display: inline-block; +} \ No newline at end of file From 397d4fb23a252ddeb8a3679e8a76916f4508c53b Mon Sep 17 00:00:00 2001 From: Doyle Date: Thu, 13 Aug 2015 13:33:04 -0400 Subject: [PATCH 21/77] Abstracted out the BasicTooboxForm so that it can be reused. --- .../src/Plugin/Block/AddThisBlock.php | 46 ++------------- .../AddThisBasicToolboxFormatter.php | 59 +++++-------------- src/AddThis.php | 53 +++++++++++++++++ 3 files changed, 73 insertions(+), 85 deletions(-) diff --git a/addthis_block/src/Plugin/Block/AddThisBlock.php b/addthis_block/src/Plugin/Block/AddThisBlock.php index 6417763..5eb0bed 100644 --- a/addthis_block/src/Plugin/Block/AddThisBlock.php +++ b/addthis_block/src/Plugin/Block/AddThisBlock.php @@ -40,6 +40,7 @@ public function defaultConfiguration() { */ public function build() { + $config = $this->configuration; $element = array( '#type' => 'addthis_wrapper', @@ -58,6 +59,7 @@ public function build() $script_manager = AddThisScriptManager::getInstance(); $script_manager->attachJsToElement($element); + $services = trim($config['share_services']); $services = str_replace(' ', '', $services); $services = explode(',', $services); @@ -132,7 +134,6 @@ public function build() $element += $items; $markup = render($element); - return array( '#markup' => $markup ); @@ -146,46 +147,11 @@ function blockForm($form, FormStateInterface $form_state) { '#title' => 'Display settings', ); + $settings = $this->getConfiguration(); + $elements = AddThis::getInstance()->getBasicToolboxForm($settings); + + $form['settings']['addthis_settings'] += $elements; - $form['settings']['addthis_settings']['share_services'] = array( - '#title' => t('Services'), - '#type' => 'textfield', - '#size' => 80, - '#default_value' => $this->configuration['share_services'], - '#required' => TRUE, - '#element_validate' => array($this, 'addThisDisplayElementServicesValidate'), - '#description' => - t('Specify the names of the sharing services and seperate them with a , (comma). The names on this list are valid.') . - t('Elements that are available but not ont the services list are (!services).', - array('!services' => 'bubble_style, pill_style, tweet, facebook_send, twitter_follow_native, google_plusone, stumbleupon_badge, counter_* (several supported services), linkedin_counter') - ), - ); - $form['settings']['addthis_settings']['buttons_size'] = array( - '#title' => t('Buttons size'), - '#type' => 'select', - '#default_value' => $this->configuration['buttons_size'], - '#options' => array( - 'addthis_16x16_style' => t('Small (16x16)'), - 'addthis_32x32_style' => t('Big (32x32)'), - ), - ); - $form['settings']['addthis_settings']['counter_orientation'] = array( - '#title' => t('Counter orientation'), - '#description' => t('Specify the way service counters are oriented.'), - '#type' => 'select', - '#default_value' => $this->configuration['counter_orientation'], - '#options' => array( - 'horizontal' => t('Horizontal'), - 'vertical' => t('Vertical'), - ) - ); - $form['settings']['addthis_settings']['extra_css'] = array( - '#title' => t('Extra CSS declaration'), - '#type' => 'textfield', - '#size' => 40, - '#default_value' => $this->configuration['extra_css'], - '#description' => t('Specify extra CSS classes to apply to the toolbox'), - ); return $form; } public function blockSubmit($form, FormStateInterface $form_state) { diff --git a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php index 9d4bd50..bd0aba8 100644 --- a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php +++ b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php @@ -23,12 +23,14 @@ * } * ) */ -class AddThisBasicToolboxFormatter extends FormatterBase { +class AddThisBasicToolboxFormatter extends FormatterBase +{ /** * {@inheritdoc} */ - public static function defaultSettings() { + public static function defaultSettings() + { return array( 'share_services' => 'facebook,twitter', 'buttons_size' => 'addthis_16x16_style', @@ -40,53 +42,19 @@ public static function defaultSettings() { /** * {@inheritdoc} */ - public function settingsForm(array $form, FormStateInterface $form_state) { + public function settingsForm(array $form, FormStateInterface $form_state) + { + + $settings = $this->getSettings(); $element = array(); - $element['share_services'] = array( - '#title' => t('Services'), - '#type' => 'textfield', - '#size' => 80, - '#default_value' => $this->getSetting('share_services'), - '#required' => TRUE, - '#element_validate' => array($this, 'addThisDisplayElementServicesValidate'), - '#description' => - t('Specify the names of the sharing services and seperate them with a , (comma). The names on this list are valid.') . - t('Elements that are available but not ont the services list are (!services).', - array('!services' => 'bubble_style, pill_style, tweet, facebook_send, twitter_follow_native, google_plusone, stumbleupon_badge, counter_* (several supported services), linkedin_counter') - ), - ); - $element['buttons_size'] = array( - '#title' => t('Buttons size'), - '#type' => 'select', - '#default_value' => $this->getSetting('buttons_size'), - '#options' => array( - 'addthis_16x16_style' => t('Small (16x16)'), - 'addthis_32x32_style' => t('Big (32x32)'), - ), - ); - $element['counter_orientation'] = array( - '#title' => t('Counter orientation'), - '#description' => t('Specify the way service counters are oriented.'), - '#type' => 'select', - '#default_value' => $this->getSetting('counter_orientation'), - '#options' => array( - 'horizontal' => t('Horizontal'), - 'vertical' => t('Vertical'), - ) - ); - $element['extra_css'] = array( - '#title' => t('Extra CSS declaration'), - '#type' => 'textfield', - '#size' => 40, - '#default_value' => $this->getSetting('extra_css'), - '#description' => t('Specify extra CSS classes to apply to the toolbox'), - ); + AddThis::getInstance()->getBasicToolboxForm($settings); return $element; } - public function addThisDisplayElementServicesValidate(array $element, FormStateInterface $form_state){ + public function addThisDisplayElementServicesValidate(array $element, FormStateInterface $form_state) + { $bad = FALSE; $services = trim($element['#value']); @@ -106,8 +74,8 @@ public function addThisDisplayElementServicesValidate(array $element, FormStateI /** * {@inheritdoc} */ - public function viewElements(FieldItemListInterface $items) { - + public function viewElements(FieldItemListInterface $items) + { $widget_settings = $this->getSettings(); $element = array( @@ -123,6 +91,7 @@ public function viewElements(FieldItemListInterface $items) { ), ); + // Add the widget script. $script_manager = AddThisScriptManager::getInstance(); $script_manager->attachJsToElement($element); diff --git a/src/AddThis.php b/src/AddThis.php index e64b0d8..5b88f70 100644 --- a/src/AddThis.php +++ b/src/AddThis.php @@ -248,6 +248,59 @@ public function getDisplayTypes() { return $displays; } + /** + * Provides options for the BasicToolboxForm. This is used in field & block + * configurations. + * @param $options + * @return array + */ + public function getBasicToolboxForm($options){ + $element = array(); + + $element['share_services'] = array( + '#title' => t('Services'), + '#type' => 'textfield', + '#size' => 80, + '#default_value' => $options['share_services'], + '#required' => TRUE, + '#element_validate' => array($this, 'addThisDisplayElementServicesValidate'), + '#description' => + t('Specify the names of the sharing services and seperate them with a , (comma). The names on this list are valid.') . + t('Elements that are available but not ont the services list are (!services).', + array('!services' => 'bubble_style, pill_style, tweet, facebook_send, twitter_follow_native, google_plusone, stumbleupon_badge, counter_* (several supported services), linkedin_counter') + ), + ); + $element['buttons_size'] = array( + '#title' => t('Buttons size'), + '#type' => 'select', + '#default_value' => $options['buttons_size'], + '#options' => array( + 'addthis_16x16_style' => t('Small (16x16)'), + 'addthis_32x32_style' => t('Big (32x32)'), + ), + ); + $element['counter_orientation'] = array( + '#title' => t('Counter orientation'), + '#description' => t('Specify the way service counters are oriented.'), + '#type' => 'select', + '#default_value' => $options['counter_orientation'], + '#options' => array( + 'horizontal' => t('Horizontal'), + 'vertical' => t('Vertical'), + ) + ); + $element['extra_css'] = array( + '#title' => t('Extra CSS declaration'), + '#type' => 'textfield', + '#size' => 40, + '#default_value' => $options['extra_css'], + '#description' => t('Specify extra CSS classes to apply to the toolbox'), + ); + + return $element; + } + + } \ No newline at end of file From 2271827b5a3ec973d37247c4597f58270384b583 Mon Sep 17 00:00:00 2001 From: Jason Enter Date: Thu, 13 Aug 2015 13:35:30 -0400 Subject: [PATCH 22/77] line up icon on admin --- css/addthis.admin.css | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/css/addthis.admin.css b/css/addthis.admin.css index f7a7bce..b604493 100644 --- a/css/addthis.admin.css +++ b/css/addthis.admin.css @@ -17,4 +17,9 @@ #edit-addthis-excluded-services .addthis_service_icon, #edit-addthis-enabled-services .addthis_service_icon { display: inline-block; +} + +#edit-addthis-excluded-services .addthis_service_icon, +#edit-addthis-enabled-services .addthis_service_icon { + margin-bottom: -1.5px; } \ No newline at end of file From f051915f5cf884ec603cfd54a4947f23169585a3 Mon Sep 17 00:00:00 2001 From: Doyle Date: Thu, 13 Aug 2015 13:40:23 -0400 Subject: [PATCH 23/77] Updated form to allow for correct validation of services defined in textfield. --- addthis.module | 23 +++++++++++++++++++ .../src/Plugin/Block/AddThisBlock.php | 2 +- .../AddThisBasicToolboxFormatter.php | 19 +-------------- src/AddThis.php | 5 ++-- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/addthis.module b/addthis.module index 9115772..1bfafce 100644 --- a/addthis.module +++ b/addthis.module @@ -42,3 +42,26 @@ function _addthis_field_info_formatter_field_type($field_type = NULL) { } +/** + * Validation for services for BasicToolbox. + * @param array $element + * @param FormStateInterface $form_state + */ +function addThisDisplayElementServicesValidate(array $element, FormStateInterface $form_state) +{ + $bad = FALSE; + + $services = trim($element['#value']); + $services = str_replace(' ', '', $services); + + if (!preg_match('/^[a-z\_\,0-9]+$/', $services)) { + $bad = TRUE; + } + // @todo Validate the service names against AddThis.com. Give a notice when there are bad names. + + // Return error. + if ($bad) { + form_error($element, t('The declared services are incorrect or nonexistent.')); + } +} + diff --git a/addthis_block/src/Plugin/Block/AddThisBlock.php b/addthis_block/src/Plugin/Block/AddThisBlock.php index 5eb0bed..10bf5e4 100644 --- a/addthis_block/src/Plugin/Block/AddThisBlock.php +++ b/addthis_block/src/Plugin/Block/AddThisBlock.php @@ -148,7 +148,7 @@ function blockForm($form, FormStateInterface $form_state) { ); $settings = $this->getConfiguration(); - $elements = AddThis::getInstance()->getBasicToolboxForm($settings); + $elements = AddThis::getInstance()->getBasicToolboxForm($this, $settings); $form['settings']['addthis_settings'] += $elements; diff --git a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php index bd0aba8..4580610 100644 --- a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php +++ b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php @@ -48,28 +48,11 @@ public function settingsForm(array $form, FormStateInterface $form_state) $settings = $this->getSettings(); $element = array(); - AddThis::getInstance()->getBasicToolboxForm($settings); + AddThis::getInstance()->getBasicToolboxForm($this, $settings); return $element; } - public function addThisDisplayElementServicesValidate(array $element, FormStateInterface $form_state) - { - $bad = FALSE; - - $services = trim($element['#value']); - $services = str_replace(' ', '', $services); - - if (!preg_match('/^[a-z\_\,0-9]+$/', $services)) { - $bad = TRUE; - } - // @todo Validate the service names against AddThis.com. Give a notice when there are bad names. - - // Return error. - if ($bad) { - form_error($element, t('The declared services are incorrect or nonexistent.')); - } - } /** * {@inheritdoc} diff --git a/src/AddThis.php b/src/AddThis.php index 5b88f70..864aaad 100644 --- a/src/AddThis.php +++ b/src/AddThis.php @@ -254,7 +254,7 @@ public function getDisplayTypes() { * @param $options * @return array */ - public function getBasicToolboxForm($options){ + public function getBasicToolboxForm($parent_class, $options){ $element = array(); $element['share_services'] = array( @@ -263,7 +263,8 @@ public function getBasicToolboxForm($options){ '#size' => 80, '#default_value' => $options['share_services'], '#required' => TRUE, - '#element_validate' => array($this, 'addThisDisplayElementServicesValidate'), + //Validate function is defined in addthis.module. + '#element_validate' => array($parent_class, 'addThisDisplayElementServicesValidate'), '#description' => t('Specify the names of the sharing services and seperate them with a , (comma). The names on this list are valid.') . t('Elements that are available but not ont the services list are (!services).', From c69546394ef4dd7532d2a84d97c4e63cae96e76c Mon Sep 17 00:00:00 2001 From: Jason Enter Date: Thu, 13 Aug 2015 14:15:04 -0400 Subject: [PATCH 24/77] fix naming --- config/schema/addthis.schema.yml | 2 +- src/Form/AddThisSettingsForm.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/schema/addthis.schema.yml b/config/schema/addthis.schema.yml index 3f107d9..dbeba51 100644 --- a/config/schema/addthis.schema.yml +++ b/config/schema/addthis.schema.yml @@ -65,7 +65,7 @@ addthis.settings: label: 'Services Excluded' analytics: type: mapping - label: 'Analytics & Tracking' + label: 'Analytics and Tracking' mapping: addthis_profile_id: type: string diff --git a/src/Form/AddThisSettingsForm.php b/src/Form/AddThisSettingsForm.php index e1f8d56..812e02e 100644 --- a/src/Form/AddThisSettingsForm.php +++ b/src/Form/AddThisSettingsForm.php @@ -172,7 +172,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { $can_track_clicks = empty($profile_id) ? FALSE : TRUE; $form['fieldset_analytics'] = array( '#type' => 'fieldset', - '#title' => t('Analytics & Tracking'), + '#title' => t('Analytics and Tracking'), '#collapsible' => TRUE, '#collapsed' => $can_track_clicks ? TRUE : FALSE, ); From 5b857a092f346ddb4d437d7f9d995f6097a5c947 Mon Sep 17 00:00:00 2001 From: Doyle Date: Thu, 13 Aug 2015 14:53:22 -0400 Subject: [PATCH 25/77] Abstracted out block to use BasicToolbox and BasicButton settings. Implemented Ajax to show/hide settings based on selection. --- .../src/Plugin/Block/AddThisBlock.php | 201 ++++++++---------- .../AddThisBasicButtonFormatter.php | 61 +----- .../AddThisBasicToolboxFormatter.php | 95 +-------- src/AddThis.php | 157 ++++++++++++++ 4 files changed, 250 insertions(+), 264 deletions(-) diff --git a/addthis_block/src/Plugin/Block/AddThisBlock.php b/addthis_block/src/Plugin/Block/AddThisBlock.php index 10bf5e4..dfe4731 100644 --- a/addthis_block/src/Plugin/Block/AddThisBlock.php +++ b/addthis_block/src/Plugin/Block/AddThisBlock.php @@ -9,7 +9,6 @@ use Drupal\Core\Block\BlockBase; use Drupal\Core\Form\FormStateInterface; use Drupal\addthis\AddThis; -use Drupal\addthis\Services\AddThisScriptManager; /** * Provides my custom block. @@ -26,139 +25,109 @@ class AddThisBlock extends BlockBase /** * {@inheritdoc} */ - public function defaultConfiguration() { + public function defaultConfiguration() + { return array( - 'share_services' => 'facebook,twitter', - 'buttons_size' => 'addthis_16x16_style', - 'counter_orientation' => 'horizontal', - 'extra_css' => '', + 'type' => 'addthis_disabled', + 'basic_toolbox' => array( + 'share_services' => 'facebook,twitter', + 'buttons_size' => 'addthis_16x16_style', + 'counter_orientation' => 'horizontal', + 'extra_css' => '', + ), + 'basic_button' => array( + 'buttons_size' => 'addthis_16x16_style', + 'extra_css' => '', + ), ); } - /** - * {@inheritdoc} - */ - public function build() + + function blockForm($form, FormStateInterface $form_state) { - $config = $this->configuration; - $element = array( - '#type' => 'addthis_wrapper', - '#tag' => 'div', - '#attributes' => array( - 'class' => array( - 'addthis_toolbox', - 'addthis_default_style', - ($config['buttons_size'] == AddThis::CSS_32x32 ? AddThis::CSS_32x32 : NULL), - $config['extra_css'], - ), - ), - ); + // The list of formatters. + $formatter_options = AddThis::getInstance()->getDisplayTypes(); + $settings = $this->getConfiguration(); - // Add the widget script. - $script_manager = AddThisScriptManager::getInstance(); - $script_manager->attachJsToElement($element); - - - $services = trim($config['share_services']); - $services = str_replace(' ', '', $services); - $services = explode(',', $services); - $items = array(); - - // All service elements - $items = array(); - foreach ($services as $service) { - $items[$service] = array( - '#type' => 'addthis_element', - '#tag' => 'a', - '#value' => '', - '#attributes' => array( - 'href' => AddThis::getInstance()->getBaseBookmarkUrl(), - 'class' => array( - 'addthis_button_' . $service, - ), - ), - '#addthis_service' => $service, - ); - - // Add individual counters. - if (strpos($service, 'counter_') === 0) { - $items[$service]['#attributes']['class'] = array("addthis_$service"); - } - - // Basic implementations of bubble counter orientation. - // @todo Figure all the bubbles out and add them. - // Still missing: tweetme, hyves and stubleupon, google_plusone_badge. - // - $orientation = ($config['counter_orientation'] == 'horizontal' ? TRUE : FALSE); - switch ($service) { - case 'linkedin_counter': - $items[$service]['#attributes'] += array( - 'li:counter' => ($orientation ? '' : 'top'), - ); - break; - case 'facebook_like': - $items[$service]['#attributes'] += array( - 'fb:like:layout' => ($orientation ? 'button_count' : 'box_count') - ); - break; - case 'facebook_share': - $items[$service]['#attributes'] += array( - 'fb:share:layout' => ($orientation ? 'button_count' : 'box_count') - ); - break; - case 'google_plusone': - $items[$service]['#attributes'] += array( - 'g:plusone:size' => ($orientation ? 'standard' : 'tall') - ); - break; - case 'tweet': - $items[$service]['#attributes'] += array( - 'tw:count' => ($orientation ? 'horizontal' : 'vertical'), - 'tw:via' => AddThis::getInstance()->getTwitterVia(), - ); - break; - case 'bubble_style': - $items[$service]['#attributes']['class'] = array( - 'addthis_counter', 'addthis_bubble_style' - ); - break; - case 'pill_style': - $items[$service]['#attributes']['class'] = array( - 'addthis_counter', 'addthis_pill_style' - ); - break; - } + $type = $settings['type']; + $rebuild = $form_state->getValue(['settings', 'settings', 'addthis_settings', 'type']); + if( isset($rebuild) ){ + $type = $form_state->getValue(['settings', 'settings', 'addthis_settings', 'type']); } - $element += $items; - - $markup = render($element); - return array( - '#markup' => $markup - ); - - } - - function blockForm($form, FormStateInterface $form_state) { - //@TODO: Implement block form. $form['settings']['addthis_settings'] = array( '#type' => 'fieldset', '#title' => 'Display settings', + ); - $settings = $this->getConfiguration(); - $elements = AddThis::getInstance()->getBasicToolboxForm($this, $settings); + $form['settings']['addthis_settings']['type'] = array( + '#type' => 'select', + '#title' => t('Formatter for @title', array('@title' => 'AddThis block')), + '#title_display' => 'invisible', + '#options' => $formatter_options, + '#default_value' => $settings['type'], + '#attributes' => array('class' => array('addthis-display-type')), + '#ajax' => array( + 'callback' => array($this, 'addthisAjaxCallback'), + 'wrapper' => 'addthis_type_settings', + ), + ); + $form['settings']['addthis_settings']['type_settings'] = array( + '#prefix' => '
'
', + ); + if($type == 'addthis_basic_toolbox'){ + $basicToolbox = AddThis::getInstance()->getBasicToolboxForm($this, $settings['basic_toolbox']); + $form['settings']['addthis_settings']['type_settings']['basic_toolbox'] = $basicToolbox; + } + else if ($type == 'addthis_basic_button'){ + $basicButton = AddThis::getInstance()->getBasicButtonForm($this, $settings['basic_button']); + $form['settings']['addthis_settings']['type_settings']['basic_button'] = $basicButton; + } - $form['settings']['addthis_settings'] += $elements; return $form; } - public function blockSubmit($form, FormStateInterface $form_state) { - $this->configuration['share_services'] = $form_state->getValue(['settings', 'addthis_settings', 'share_services']); - $this->configuration['buttons_size'] = $form_state->getValue(['settings', 'addthis_settings', 'buttons_size']); - $this->configuration['counter_orientation'] = $form_state->getValue(['settings', 'addthis_settings', 'counter_orientation']); - $this->configuration['extra_css'] = $form_state->getValue(['settings', 'addthis_settings', 'extra_css']); + + public function addthisAjaxCallback(array $form, FormStateInterface $form_state){ + return $form['settings']['settings']['addthis_settings']['type_settings']; + } + + public function blockSubmit($form, FormStateInterface $form_state) + { + $this->configuration['type'] = $form_state->getValue(['settings', 'addthis_settings', 'type']); + $this->configuration['basic_toolbox']['share_services'] = $form_state->getValue(['settings', 'addthis_settings', 'type_settings', 'basic_toolbox', 'share_services']); + $this->configuration['basic_toolbox']['buttons_size'] = $form_state->getValue(['settings', 'addthis_settings', 'type_settings', 'basic_toolbox', 'buttons_size']); + $this->configuration['basic_toolbox']['counter_orientation'] = $form_state->getValue(['settings', 'addthis_settings', 'type_settings', 'basic_toolbox', 'counter_orientation']); + $this->configuration['basic_toolbox']['extra_css'] = $form_state->getValue(['settings', 'addthis_settings', 'type_settings', 'basic_toolbox', 'extra_css']); + $this->configuration['basic_button']['button_size'] = $form_state->getValue(['settings', 'addthis_settings', 'type_settings', 'basic_button', 'button_size']); + $this->configuration['basic_button']['extra_css'] = $form_state->getValue(['settings', 'addthis_settings', 'type_settings', 'basic_button', 'extra_css']); + + } + + /** + * {@inheritdoc} + */ + public function build() + { + $config = $this->configuration; + switch($config['type']){ + case 'addthis_basic_button': + $markup = AddThis::getInstance()->getBasicButtonMarkup($config['basic_button']); + break; + case 'addthis_basic_toolbox': + $markup = AddThis::getInstance()->getBasicToolboxMarkup($config['basic_toolbox']); + break; + default: + $markup = ''; + break; + } + + return array( + '#markup' => $markup + ); } diff --git a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php index f77b9dd..1a9d215 100644 --- a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php +++ b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php @@ -10,7 +10,6 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\addthis\AddThis; -use Drupal\addthis\Services\AddThisScriptManager; /** @@ -40,24 +39,9 @@ public static function defaultSettings() { * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state) { - $element = array(); + $settings = $this->getSettings(); - $element['button_size'] = array( - '#title' => t('Image'), - '#type' => 'select', - '#default_value' => $this->getSetting('button_size'), - '#options' => array( - 'small' => t('Small'), - 'big' => t('Big'), - ), - ); - $element['extra_css'] = array( - '#title' => t('Extra CSS declaration'), - '#type' => 'textfield', - '#size' => 40, - '#default_value' => $this->getSetting('extra_css'), - '#description' => t('Specify extra CSS classes to apply to the button'), - ); + $element = AddThis::getInstance()->getBasicButtonForm($this, $settings); return $element; } @@ -67,44 +51,11 @@ public function settingsForm(array $form, FormStateInterface $form_state) { * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items) { - //@TODO Implement viewElements() - $addthis = AddThis::getInstance(); - //$settings = $options['#display']['settings']; - - $button_img = 'http://s7.addthis.com/static/btn/sm-share-en.gif'; - if (isset($settings['buttons_size']) && $settings['buttons_size'] == 'big') { - $button_img = 'http://s7.addthis.com/static/btn/v2/lg-share-en.gif'; - } - //$button_img = $addthis->transformToSecureUrl($button_img); - - //$extra_css = isset($settings['extra_css']) ? $settings['extra_css'] : ''; - $element = array( - '#type' => 'addthis_wrapper', - '#tag' => 'a', - '#attributes' => array( - 'class' => array( - 'addthis_button', - ), - ), - ); - //$element['#attributes'] += $addthis->getAddThisAttributesMarkup($options); - - // Add the widget script. - $script_manager = AddThisScriptManager::getInstance(); - $script_manager->attachJsToElement($element); - - // Create img button. - $image = array( - '#type' => 'addthis_element', - '#tag' => 'img', - '#attributes' => array( - 'src' => $button_img, - 'alt' => t('Share page with AddThis'), - ), + $settings = $this->getSettings(); + $markup = AddThis::getInstance()->getBasicButtonMarkup($settings); + return array( + '#markup' => $markup ); - $element[] = $image; - - return $element; } diff --git a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php index 4580610..2bd117d 100644 --- a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php +++ b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php @@ -10,7 +10,6 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\addthis\AddThis; -use Drupal\addthis\Services\AddThisScriptManager; /** * Plugin implementation of the 'addthis_basic_toolbox' formatter. @@ -59,100 +58,10 @@ public function settingsForm(array $form, FormStateInterface $form_state) */ public function viewElements(FieldItemListInterface $items) { - $widget_settings = $this->getSettings(); - - $element = array( - '#type' => 'addthis_wrapper', - '#tag' => 'div', - '#attributes' => array( - 'class' => array( - 'addthis_toolbox', - 'addthis_default_style', - ($widget_settings['buttons_size'] == AddThis::CSS_32x32 ? AddThis::CSS_32x32 : NULL), - $widget_settings['extra_css'], - ), - ), - ); - - - // Add the widget script. - $script_manager = AddThisScriptManager::getInstance(); - $script_manager->attachJsToElement($element); - - $services = trim($widget_settings['share_services']); - $services = str_replace(' ', '', $services); - $services = explode(',', $services); - $items = array(); - - // All service elements - $items = array(); - foreach ($services as $service) { - $items[$service] = array( - '#type' => 'addthis_element', - '#tag' => 'a', - '#value' => '', - '#attributes' => array( - 'href' => AddThis::getInstance()->getBaseBookmarkUrl(), - 'class' => array( - 'addthis_button_' . $service, - ), - ), - '#addthis_service' => $service, - ); - - // Add individual counters. - if (strpos($service, 'counter_') === 0) { - $items[$service]['#attributes']['class'] = array("addthis_$service"); - } - - // Basic implementations of bubble counter orientation. - // @todo Figure all the bubbles out and add them. - // Still missing: tweetme, hyves and stubleupon, google_plusone_badge. - // - $orientation = ($widget_settings['counter_orientation'] == 'horizontal' ? TRUE : FALSE); - switch ($service) { - case 'linkedin_counter': - $items[$service]['#attributes'] += array( - 'li:counter' => ($orientation ? '' : 'top'), - ); - break; - case 'facebook_like': - $items[$service]['#attributes'] += array( - 'fb:like:layout' => ($orientation ? 'button_count' : 'box_count') - ); - break; - case 'facebook_share': - $items[$service]['#attributes'] += array( - 'fb:share:layout' => ($orientation ? 'button_count' : 'box_count') - ); - break; - case 'google_plusone': - $items[$service]['#attributes'] += array( - 'g:plusone:size' => ($orientation ? 'standard' : 'tall') - ); - break; - case 'tweet': - $items[$service]['#attributes'] += array( - 'tw:count' => ($orientation ? 'horizontal' : 'vertical'), - 'tw:via' => AddThis::getInstance()->getTwitterVia(), - ); - break; - case 'bubble_style': - $items[$service]['#attributes']['class'] = array( - 'addthis_counter', 'addthis_bubble_style' - ); - break; - case 'pill_style': - $items[$service]['#attributes']['class'] = array( - 'addthis_counter', 'addthis_pill_style' - ); - break; - } - } + $settings = $this->getSettings(); - $element += $items; + $markup = AddThis::getInstance()->getBasicToolboxMarkup($settings); - $markup = render($element); return array( '#markup' => $markup ); diff --git a/src/AddThis.php b/src/AddThis.php index 864aaad..4e4e00a 100644 --- a/src/AddThis.php +++ b/src/AddThis.php @@ -8,6 +8,7 @@ use Drupal\addthis\Util\AddThisJson; use Drupal\Component\Utility\SafeMarkup; +use Drupal\addthis\Services\AddThisScriptManager; class AddThis { const BLOCK_NAME = 'addthis_block'; @@ -302,6 +303,162 @@ public function getBasicToolboxForm($parent_class, $options){ } + public function getBasicButtonForm($parent_class, $options){ + $element = array(); + + $element['button_size'] = array( + '#title' => t('Image'), + '#type' => 'select', + '#default_value' => $options['button_size'], + '#options' => array( + 'small' => t('Small'), + 'big' => t('Big'), + ), + ); + $element['extra_css'] = array( + '#title' => t('Extra CSS declaration'), + '#type' => 'textfield', + '#size' => 40, + '#default_value' => $options['extra_css'], + '#description' => t('Specify extra CSS classes to apply to the button'), + ); + + return $element; + } + + + function getBasicToolboxMarkup($settings){ + $element = array( + '#type' => 'addthis_wrapper', + '#tag' => 'div', + '#attributes' => array( + 'class' => array( + 'addthis_toolbox', + 'addthis_default_style', + ($settings['buttons_size'] == AddThis::CSS_32x32 ? AddThis::CSS_32x32 : NULL), + $settings['extra_css'], + ), + ), + ); + + // Add the widget script. + $script_manager = AddThisScriptManager::getInstance(); + $script_manager->attachJsToElement($element); + + + $services = trim($settings['share_services']); + $services = str_replace(' ', '', $services); + $services = explode(',', $services); + $items = array(); + + // All service elements + $items = array(); + foreach ($services as $service) { + $items[$service] = array( + '#type' => 'addthis_element', + '#tag' => 'a', + '#value' => '', + '#attributes' => array( + 'href' => AddThis::getInstance()->getBaseBookmarkUrl(), + 'class' => array( + 'addthis_button_' . $service, + ), + ), + '#addthis_service' => $service, + ); + + // Add individual counters. + if (strpos($service, 'counter_') === 0) { + $items[$service]['#attributes']['class'] = array("addthis_$service"); + } + + // Basic implementations of bubble counter orientation. + // @todo Figure all the bubbles out and add them. + // Still missing: tweetme, hyves and stubleupon, google_plusone_badge. + // + $orientation = ($settings['counter_orientation'] == 'horizontal' ? TRUE : FALSE); + switch ($service) { + case 'linkedin_counter': + $items[$service]['#attributes'] += array( + 'li:counter' => ($orientation ? '' : 'top'), + ); + break; + case 'facebook_like': + $items[$service]['#attributes'] += array( + 'fb:like:layout' => ($orientation ? 'button_count' : 'box_count') + ); + break; + case 'facebook_share': + $items[$service]['#attributes'] += array( + 'fb:share:layout' => ($orientation ? 'button_count' : 'box_count') + ); + break; + case 'google_plusone': + $items[$service]['#attributes'] += array( + 'g:plusone:size' => ($orientation ? 'standard' : 'tall') + ); + break; + case 'tweet': + $items[$service]['#attributes'] += array( + 'tw:count' => ($orientation ? 'horizontal' : 'vertical'), + 'tw:via' => AddThis::getInstance()->getTwitterVia(), + ); + break; + case 'bubble_style': + $items[$service]['#attributes']['class'] = array( + 'addthis_counter', 'addthis_bubble_style' + ); + break; + case 'pill_style': + $items[$service]['#attributes']['class'] = array( + 'addthis_counter', 'addthis_pill_style' + ); + break; + } + } + + $element += $items; + + return render($element); + } + + + function getBasicButtonMarkup($settings){ + $button_img = 'http://s7.addthis.com/static/btn/sm-share-en.gif'; + if (isset($settings['buttons_size']) && $settings['buttons_size'] == 'big') { + $button_img = 'http://s7.addthis.com/static/btn/v2/lg-share-en.gif'; + } + //$button_img = $addthis->transformToSecureUrl($button_img); + + //$extra_css = isset($settings['extra_css']) ? $settings['extra_css'] : ''; + $element = array( + '#type' => 'addthis_wrapper', + '#tag' => 'a', + '#attributes' => array( + 'class' => array( + 'addthis_button', + ), + ), + ); + //$element['#attributes'] += $addthis->getAddThisAttributesMarkup($options); + + // Add the widget script. + $script_manager = AddThisScriptManager::getInstance(); + $script_manager->attachJsToElement($element); + + // Create img button. + $image = array( + '#type' => 'addthis_element', + '#tag' => 'img', + '#attributes' => array( + 'src' => $button_img, + 'alt' => t('Share page with AddThis'), + ), + ); + $element[] = $image; + + return render($element); + } } \ No newline at end of file From 044eeff782dc06de0f8884193b342db15ee305c1 Mon Sep 17 00:00:00 2001 From: Doyle Date: Thu, 13 Aug 2015 15:26:31 -0400 Subject: [PATCH 26/77] Added comments per notes on PR. --- .../src/Plugin/Block/AddThisBlock.php | 13 +++++++++++- src/AddThis.php | 21 ++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/addthis_block/src/Plugin/Block/AddThisBlock.php b/addthis_block/src/Plugin/Block/AddThisBlock.php index dfe4731..25c706a 100644 --- a/addthis_block/src/Plugin/Block/AddThisBlock.php +++ b/addthis_block/src/Plugin/Block/AddThisBlock.php @@ -42,7 +42,9 @@ public function defaultConfiguration() ); } - + /** + * {@inheritdoc} + */ function blockForm($form, FormStateInterface $form_state) { @@ -91,10 +93,19 @@ function blockForm($form, FormStateInterface $form_state) return $form; } + /** + * Callback for AddThisBlock blockForm() to control sub-settings based on display type. + * @param array $form + * @param FormStateInterface $form_state + * @return mixed + */ public function addthisAjaxCallback(array $form, FormStateInterface $form_state){ return $form['settings']['settings']['addthis_settings']['type_settings']; } + /** + * {@inheritdoc} + */ public function blockSubmit($form, FormStateInterface $form_state) { $this->configuration['type'] = $form_state->getValue(['settings', 'addthis_settings', 'type']); diff --git a/src/AddThis.php b/src/AddThis.php index 4e4e00a..18d3f84 100644 --- a/src/AddThis.php +++ b/src/AddThis.php @@ -303,6 +303,14 @@ public function getBasicToolboxForm($parent_class, $options){ } + /** + * + * Returns the basicButtonForm elements to be used in the Field and Block implementation. + * + * @param $parent_class + * @param $options + * @return array + */ public function getBasicButtonForm($parent_class, $options){ $element = array(); @@ -326,7 +334,12 @@ public function getBasicButtonForm($parent_class, $options){ return $element; } - + /** + * Returns rendered markup for the BasicToolbox display. This will be called + * from both the Field and Block render functions. + * @param $settings + * @return null + */ function getBasicToolboxMarkup($settings){ $element = array( '#type' => 'addthis_wrapper', @@ -423,6 +436,12 @@ function getBasicToolboxMarkup($settings){ } + /** + * Returns rendered markup for the BasicButton display. This will be called + * from both the Field and Block render functions. + * @param $settings + * @return null + */ function getBasicButtonMarkup($settings){ $button_img = 'http://s7.addthis.com/static/btn/sm-share-en.gif'; if (isset($settings['buttons_size']) && $settings['buttons_size'] == 'big') { From 221e17af5cb3449a3e64355bf00308fe3fa30f0c Mon Sep 17 00:00:00 2001 From: Doyle Date: Thu, 13 Aug 2015 15:30:29 -0400 Subject: [PATCH 27/77] Reformatted code per drupal standards --- addthis.module | 10 +- .../src/Plugin/Block/AddThisBlock.php | 110 +++++++++---- .../AddThisBasicButtonFormatter.php | 2 - .../AddThisBasicToolboxFormatter.php | 12 +- css/addthis.admin.css | 12 +- js/addthis.js | 151 +++++++++--------- src/AddThis.php | 32 ++-- src/Element/AddThisElement.php | 6 +- src/Element/AddThisWrapper.php | 15 +- src/Form/AddThisSettingsAdvancedForm.php | 3 +- src/Form/AddThisSettingsForm.php | 7 +- .../Field/FieldFormatter/AddThisFormatter.php | 27 ++-- src/Plugin/Field/FieldType/AddThisItem.php | 9 +- .../Field/FieldWidget/AddThisWidget.php | 3 +- src/Services/AddThisScriptManager.php | 19 ++- src/Util/AddThisJson.php | 3 +- 16 files changed, 242 insertions(+), 179 deletions(-) diff --git a/addthis.module b/addthis.module index 1bfafce..0679aba 100644 --- a/addthis.module +++ b/addthis.module @@ -18,12 +18,12 @@ function addthis_theme() { * @TODO Find another way to do this since it breaks aggregation. * @param array $page */ -function addthis_page_attachments(array &$page){ +function addthis_page_attachments(array &$page) { $page['#attached']['library'][] = 'addthis/addthis.global'; } -function addthis_library_info_alter(){ +function addthis_library_info_alter() { } @@ -32,7 +32,8 @@ function addthis_library_info_alter(){ * Implementation to retrieve formatters for a given type of field. */ function _addthis_field_info_formatter_field_type($field_type = NULL) { - $formatters = \Drupal::Service('plugin.manager.field.formatter')->getDefinitions(); + $formatters = \Drupal::Service('plugin.manager.field.formatter') + ->getDefinitions(); foreach ($formatters as $key => $formatter) { if (!in_array((!isset($field_type) ? 'addthis' : $field_type), $formatter['field_types'])) { unset($formatters[$key]); @@ -47,8 +48,7 @@ function _addthis_field_info_formatter_field_type($field_type = NULL) { * @param array $element * @param FormStateInterface $form_state */ -function addThisDisplayElementServicesValidate(array $element, FormStateInterface $form_state) -{ +function addThisDisplayElementServicesValidate(array $element, FormStateInterface $form_state) { $bad = FALSE; $services = trim($element['#value']); diff --git a/addthis_block/src/Plugin/Block/AddThisBlock.php b/addthis_block/src/Plugin/Block/AddThisBlock.php index 25c706a..777e423 100644 --- a/addthis_block/src/Plugin/Block/AddThisBlock.php +++ b/addthis_block/src/Plugin/Block/AddThisBlock.php @@ -19,14 +19,12 @@ * category = @Translation("Blocks") * ) */ -class AddThisBlock extends BlockBase -{ +class AddThisBlock extends BlockBase { /** * {@inheritdoc} */ - public function defaultConfiguration() - { + public function defaultConfiguration() { return array( 'type' => 'addthis_disabled', 'basic_toolbox' => array( @@ -45,17 +43,26 @@ public function defaultConfiguration() /** * {@inheritdoc} */ - function blockForm($form, FormStateInterface $form_state) - { + function blockForm($form, FormStateInterface $form_state) { // The list of formatters. $formatter_options = AddThis::getInstance()->getDisplayTypes(); $settings = $this->getConfiguration(); $type = $settings['type']; - $rebuild = $form_state->getValue(['settings', 'settings', 'addthis_settings', 'type']); - if( isset($rebuild) ){ - $type = $form_state->getValue(['settings', 'settings', 'addthis_settings', 'type']); + $rebuild = $form_state->getValue([ + 'settings', + 'settings', + 'addthis_settings', + 'type' + ]); + if (isset($rebuild)) { + $type = $form_state->getValue([ + 'settings', + 'settings', + 'addthis_settings', + 'type' + ]); } $form['settings']['addthis_settings'] = array( @@ -80,13 +87,17 @@ function blockForm($form, FormStateInterface $form_state) '#prefix' => '
'
', ); - if($type == 'addthis_basic_toolbox'){ - $basicToolbox = AddThis::getInstance()->getBasicToolboxForm($this, $settings['basic_toolbox']); + if ($type == 'addthis_basic_toolbox') { + $basicToolbox = AddThis::getInstance() + ->getBasicToolboxForm($this, $settings['basic_toolbox']); $form['settings']['addthis_settings']['type_settings']['basic_toolbox'] = $basicToolbox; } - else if ($type == 'addthis_basic_button'){ - $basicButton = AddThis::getInstance()->getBasicButtonForm($this, $settings['basic_button']); - $form['settings']['addthis_settings']['type_settings']['basic_button'] = $basicButton; + else { + if ($type == 'addthis_basic_button') { + $basicButton = AddThis::getInstance() + ->getBasicButtonForm($this, $settings['basic_button']); + $form['settings']['addthis_settings']['type_settings']['basic_button'] = $basicButton; + } } @@ -99,37 +110,77 @@ function blockForm($form, FormStateInterface $form_state) * @param FormStateInterface $form_state * @return mixed */ - public function addthisAjaxCallback(array $form, FormStateInterface $form_state){ + public function addthisAjaxCallback(array $form, FormStateInterface $form_state) { return $form['settings']['settings']['addthis_settings']['type_settings']; } /** * {@inheritdoc} */ - public function blockSubmit($form, FormStateInterface $form_state) - { - $this->configuration['type'] = $form_state->getValue(['settings', 'addthis_settings', 'type']); - $this->configuration['basic_toolbox']['share_services'] = $form_state->getValue(['settings', 'addthis_settings', 'type_settings', 'basic_toolbox', 'share_services']); - $this->configuration['basic_toolbox']['buttons_size'] = $form_state->getValue(['settings', 'addthis_settings', 'type_settings', 'basic_toolbox', 'buttons_size']); - $this->configuration['basic_toolbox']['counter_orientation'] = $form_state->getValue(['settings', 'addthis_settings', 'type_settings', 'basic_toolbox', 'counter_orientation']); - $this->configuration['basic_toolbox']['extra_css'] = $form_state->getValue(['settings', 'addthis_settings', 'type_settings', 'basic_toolbox', 'extra_css']); - $this->configuration['basic_button']['button_size'] = $form_state->getValue(['settings', 'addthis_settings', 'type_settings', 'basic_button', 'button_size']); - $this->configuration['basic_button']['extra_css'] = $form_state->getValue(['settings', 'addthis_settings', 'type_settings', 'basic_button', 'extra_css']); + public function blockSubmit($form, FormStateInterface $form_state) { + $this->configuration['type'] = $form_state->getValue([ + 'settings', + 'addthis_settings', + 'type' + ]); + $this->configuration['basic_toolbox']['share_services'] = $form_state->getValue([ + 'settings', + 'addthis_settings', + 'type_settings', + 'basic_toolbox', + 'share_services' + ]); + $this->configuration['basic_toolbox']['buttons_size'] = $form_state->getValue([ + 'settings', + 'addthis_settings', + 'type_settings', + 'basic_toolbox', + 'buttons_size' + ]); + $this->configuration['basic_toolbox']['counter_orientation'] = $form_state->getValue([ + 'settings', + 'addthis_settings', + 'type_settings', + 'basic_toolbox', + 'counter_orientation' + ]); + $this->configuration['basic_toolbox']['extra_css'] = $form_state->getValue([ + 'settings', + 'addthis_settings', + 'type_settings', + 'basic_toolbox', + 'extra_css' + ]); + $this->configuration['basic_button']['button_size'] = $form_state->getValue([ + 'settings', + 'addthis_settings', + 'type_settings', + 'basic_button', + 'button_size' + ]); + $this->configuration['basic_button']['extra_css'] = $form_state->getValue([ + 'settings', + 'addthis_settings', + 'type_settings', + 'basic_button', + 'extra_css' + ]); } /** * {@inheritdoc} */ - public function build() - { + public function build() { $config = $this->configuration; - switch($config['type']){ + switch ($config['type']) { case 'addthis_basic_button': - $markup = AddThis::getInstance()->getBasicButtonMarkup($config['basic_button']); + $markup = AddThis::getInstance() + ->getBasicButtonMarkup($config['basic_button']); break; case 'addthis_basic_toolbox': - $markup = AddThis::getInstance()->getBasicToolboxMarkup($config['basic_toolbox']); + $markup = AddThis::getInstance() + ->getBasicToolboxMarkup($config['basic_toolbox']); break; default: $markup = ''; @@ -143,7 +194,6 @@ public function build() } - } ?> \ No newline at end of file diff --git a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php index 1a9d215..c1d5160 100644 --- a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php +++ b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php @@ -59,6 +59,4 @@ public function viewElements(FieldItemListInterface $items) { } - - } \ No newline at end of file diff --git a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php index 2bd117d..d437e9c 100644 --- a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php +++ b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php @@ -22,14 +22,12 @@ * } * ) */ -class AddThisBasicToolboxFormatter extends FormatterBase -{ +class AddThisBasicToolboxFormatter extends FormatterBase { /** * {@inheritdoc} */ - public static function defaultSettings() - { + public static function defaultSettings() { return array( 'share_services' => 'facebook,twitter', 'buttons_size' => 'addthis_16x16_style', @@ -41,8 +39,7 @@ public static function defaultSettings() /** * {@inheritdoc} */ - public function settingsForm(array $form, FormStateInterface $form_state) - { + public function settingsForm(array $form, FormStateInterface $form_state) { $settings = $this->getSettings(); $element = array(); @@ -56,8 +53,7 @@ public function settingsForm(array $form, FormStateInterface $form_state) /** * {@inheritdoc} */ - public function viewElements(FieldItemListInterface $items) - { + public function viewElements(FieldItemListInterface $items) { $settings = $this->getSettings(); $markup = AddThis::getInstance()->getBasicToolboxMarkup($settings); diff --git a/css/addthis.admin.css b/css/addthis.admin.css index 99cd919..a5ace1e 100644 --- a/css/addthis.admin.css +++ b/css/addthis.admin.css @@ -1,11 +1,13 @@ .addthis_service_icon { - display: inline-block; - padding-left: 20px; + display: inline-block; + padding-left: 20px; } + #edit-addthis-enabled-services .form-item, #edit-addthis-excluded-services .form-item { - float: left; - width: 20%; + float: left; + width: 20%; } + #edit-addthis-enabled-services .form-item .description { - clear: both; + clear: both; } diff --git a/js/addthis.js b/js/addthis.js index b845355..a87c593 100644 --- a/js/addthis.js +++ b/js/addthis.js @@ -4,94 +4,93 @@ */ (function ($, Drupal, drupalSettings) { - Drupal.behaviors.addthis = { - scriptLoaded: false, - attach: function(context, settings) { + Drupal.behaviors.addthis = { + scriptLoaded: false, + attach: function (context, settings) { - // The addthis configuration is not loaded but the settings are passed - // to do so. - if (!this.isConfigLoaded() && this.isSettingsLoaded()) { - // Initialize config. - this.initConfig(); + // The addthis configuration is not loaded but the settings are passed + // to do so. + if (!this.isConfigLoaded() && this.isSettingsLoaded()) { + // Initialize config. + this.initConfig(); - // Load widget js. - if (!this.scriptLoaded) { - this.loadDomready(); - } - } - // The addthis configuration is not loaded but no setting are available - // to load anything. - else if(!this.isConfigLoaded() && !this.isSettingsLoaded()) { - // Do nothing - } + // Load widget js. + if (!this.scriptLoaded) { + this.loadDomready(); + } + } + // The addthis configuration is not loaded but no setting are available + // to load anything. + else if (!this.isConfigLoaded() && !this.isSettingsLoaded()) { + // Do nothing + } - // Trigger ready on ajax attach. - if (context != window.document) { - Drupal.behaviors.addthis.ajaxLoad(context, settings); - } + // Trigger ready on ajax attach. + if (context != window.document) { + Drupal.behaviors.addthis.ajaxLoad(context, settings); + } - }, + }, - // Returns if the settings defined by the addthis module are loaded. - isSettingsLoaded: function () { - if (typeof drupalSettings.addthis == 'undefined') { - return false; - } - return true; - }, + // Returns if the settings defined by the addthis module are loaded. + isSettingsLoaded: function () { + if (typeof drupalSettings.addthis == 'undefined') { + return false; + } + return true; + }, - // Returns if the configuration variables needed for widget.js are defined. - isConfigLoaded: function() { - if (typeof addthis_config == 'undefined' || typeof addthis_share == 'undefined') { - return false; - } - return true; - }, + // Returns if the configuration variables needed for widget.js are defined. + isConfigLoaded: function () { + if (typeof addthis_config == 'undefined' || typeof addthis_share == 'undefined') { + return false; + } + return true; + }, - initConfig: function () { - addthis_config = drupalSettings.addthis.addthis_config; - addthis_share = drupalSettings.addthis.addthis_share; - }, + initConfig: function () { + addthis_config = drupalSettings.addthis.addthis_config; + addthis_share = drupalSettings.addthis.addthis_share; + }, - // Load the js library when the dom is ready. - loadDomready: function() { - // If settings asks for loading the script after the dom is loaded, then - // load the script here. - if (!this.scriptLoaded && - this.isConfigLoaded() && - drupalSettings.addthis.domready) { - $.getScript(drupalSettings.addthis.widget_url, Drupal.behaviors.addthis.scriptReady); - } - }, + // Load the js library when the dom is ready. + loadDomready: function () { + // If settings asks for loading the script after the dom is loaded, then + // load the script here. + if (!this.scriptLoaded && + this.isConfigLoaded() && + drupalSettings.addthis.domready) { + $.getScript(drupalSettings.addthis.widget_url, Drupal.behaviors.addthis.scriptReady); + } + }, - // Callback for loading the widget.js dynamically. - scriptReady: function () { - this.scriptLoaded = true; - }, + // Callback for loading the widget.js dynamically. + scriptReady: function () { + this.scriptLoaded = true; + }, - // Called when a ajax request returned. - ajaxLoad: function(context, settings) { - if (typeof window.addthis != 'undefined' && - typeof window.addthis.toolbox == 'function') - { - window.addthis.toolbox('.addthis_toolbox'); - } - } + // Called when a ajax request returned. + ajaxLoad: function (context, settings) { + if (typeof window.addthis != 'undefined' && + typeof window.addthis.toolbox == 'function') { + window.addthis.toolbox('.addthis_toolbox'); + } + } - }; + }; - // This load the config in time to run any addthis functionality. - if (Drupal.behaviors.addthis.isConfigLoaded()) { - addthis_config = drupalSettings.addthis.addthis_config; - addthis_share = drupalSettings.addthis.addthis_share; - } + // This load the config in time to run any addthis functionality. + if (Drupal.behaviors.addthis.isConfigLoaded()) { + addthis_config = drupalSettings.addthis.addthis_config; + addthis_share = drupalSettings.addthis.addthis_share; + } - // Document ready in case we want to load AddThis into the dom after every - // thing is loaded. - // - // Is executed once after the attach happened. - $(document).ready(function() { - Drupal.behaviors.addthis.loadDomready(); - }); + // Document ready in case we want to load AddThis into the dom after every + // thing is loaded. + // + // Is executed once after the attach happened. + $(document).ready(function () { + Drupal.behaviors.addthis.loadDomready(); + }); }(jQuery, Drupal, drupalSettings)); diff --git a/src/AddThis.php b/src/AddThis.php index 18d3f84..6981a2e 100644 --- a/src/AddThis.php +++ b/src/AddThis.php @@ -100,17 +100,19 @@ public function setJson(AddThisJson $json) { $this->json = $json; } - public function setConfig(){ + public function setConfig() { $this->config = \Drupal::config('addthis.settings'); } - public function getServices() { $rows = array(); $services = $this->json->decode($this->getServicesJsonUrl()); if (empty($services)) { - drupal_set_message(t('AddThis services could not be loaded from @service_url', array('@service_url', $this->getServicesJsonUrl())), 'warning'); + drupal_set_message(t('AddThis services could not be loaded from @service_url', array( + '@service_url', + $this->getServicesJsonUrl() + )), 'warning'); } else { foreach ($services['data'] as $service) { @@ -137,7 +139,8 @@ public function getBlockDisplaySettings($widget_type) { $settings = $block_widget_settings; if ($settings == NULL && $widget_type != self::WIDGET_TYPE_DISABLED) { - $settings = \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings($widget_type); + $settings = \Drupal::service('plugin.manager.field.formatter') + ->getDefaultSettings($widget_type); } @@ -158,7 +161,6 @@ public function getServicesJsonUrl() { } - /** * Return the type of inclusion. * @@ -222,7 +224,6 @@ public function getBaseBookmarkUrl() { } - public function getFullBookmarkUrl() { return $this->getBaseBookmarkUrl() . $this->getProfileIdQueryParameterPrefixedWithAmp(); } @@ -255,7 +256,7 @@ public function getDisplayTypes() { * @param $options * @return array */ - public function getBasicToolboxForm($parent_class, $options){ + public function getBasicToolboxForm($parent_class, $options) { $element = array(); $element['share_services'] = array( @@ -265,7 +266,10 @@ public function getBasicToolboxForm($parent_class, $options){ '#default_value' => $options['share_services'], '#required' => TRUE, //Validate function is defined in addthis.module. - '#element_validate' => array($parent_class, 'addThisDisplayElementServicesValidate'), + '#element_validate' => array( + $parent_class, + 'addThisDisplayElementServicesValidate' + ), '#description' => t('Specify the names of the sharing services and seperate them with a , (comma). The names on this list are valid.') . t('Elements that are available but not ont the services list are (!services).', @@ -311,7 +315,7 @@ public function getBasicToolboxForm($parent_class, $options){ * @param $options * @return array */ - public function getBasicButtonForm($parent_class, $options){ + public function getBasicButtonForm($parent_class, $options) { $element = array(); $element['button_size'] = array( @@ -340,7 +344,7 @@ public function getBasicButtonForm($parent_class, $options){ * @param $settings * @return null */ - function getBasicToolboxMarkup($settings){ + function getBasicToolboxMarkup($settings) { $element = array( '#type' => 'addthis_wrapper', '#tag' => 'div', @@ -419,12 +423,14 @@ function getBasicToolboxMarkup($settings){ break; case 'bubble_style': $items[$service]['#attributes']['class'] = array( - 'addthis_counter', 'addthis_bubble_style' + 'addthis_counter', + 'addthis_bubble_style' ); break; case 'pill_style': $items[$service]['#attributes']['class'] = array( - 'addthis_counter', 'addthis_pill_style' + 'addthis_counter', + 'addthis_pill_style' ); break; } @@ -442,7 +448,7 @@ function getBasicToolboxMarkup($settings){ * @param $settings * @return null */ - function getBasicButtonMarkup($settings){ + function getBasicButtonMarkup($settings) { $button_img = 'http://s7.addthis.com/static/btn/sm-share-en.gif'; if (isset($settings['buttons_size']) && $settings['buttons_size'] == 'big') { $button_img = 'http://s7.addthis.com/static/btn/v2/lg-share-en.gif'; diff --git a/src/Element/AddThisElement.php b/src/Element/AddThisElement.php index bde22f0..e053d3c 100644 --- a/src/Element/AddThisElement.php +++ b/src/Element/AddThisElement.php @@ -36,11 +36,11 @@ public function getInfo() { * @param $element * @return mixed */ - public static function preRenderAddThisElement($element){ + public static function preRenderAddThisElement($element) { if (!isset($element['#value'])) { $element['addthis_element'] = [ '#markup' => '<' . $element['#tag'] . new Attribute($element['#attributes']) . " />\n" - ]; + ]; return $element; } @@ -55,7 +55,7 @@ public static function preRenderAddThisElement($element){ $output .= '\n"; $element['addthis_element'] = [ '#markup' => $output, - ]; + ]; return $element; } diff --git a/src/Element/AddThisWrapper.php b/src/Element/AddThisWrapper.php index 3dbb8fb..dfb6d90 100644 --- a/src/Element/AddThisWrapper.php +++ b/src/Element/AddThisWrapper.php @@ -9,6 +9,7 @@ use Drupal\Core\Render\Element\RenderElement; use Drupal\Core\Template\Attribute; use Drupal\Core\Render\Element; + /** * Class AddThisElement * @@ -35,18 +36,18 @@ public function getInfo() { * @param $element * @return mixed */ - public static function preRenderAddThisWrapper($element){ + public static function preRenderAddThisWrapper($element) { $output = '<' . $element['#tag'] . new Attribute($element['#attributes']) . '>'; $children = Element::children($element); - if (count($children) > 0) { - foreach ($children as $child) { - $output .= render($element[$child]); - } - } + if (count($children) > 0) { + foreach ($children as $child) { + $output .= render($element[$child]); + } + } $output .= ' \n"; $element['addthis_wrapper'] = [ '#markup' => $output - ]; + ]; return $element; } diff --git a/src/Form/AddThisSettingsAdvancedForm.php b/src/Form/AddThisSettingsAdvancedForm.php index 958bfe7..5a6db82 100644 --- a/src/Form/AddThisSettingsAdvancedForm.php +++ b/src/Form/AddThisSettingsAdvancedForm.php @@ -76,7 +76,8 @@ public function buildForm(array $form, FormStateInterface $form_state) { $form['advanced_settings_fieldset'] = array( '#type' => 'fieldset', '#title' => t('Advanced settings'), - '#access' => \Drupal::currentUser()->hasPermission('administer advanced addthis'), + '#access' => \Drupal::currentUser() + ->hasPermission('administer advanced addthis'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); diff --git a/src/Form/AddThisSettingsForm.php b/src/Form/AddThisSettingsForm.php index e1f8d56..5ceaf06 100644 --- a/src/Form/AddThisSettingsForm.php +++ b/src/Form/AddThisSettingsForm.php @@ -167,7 +167,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#columns' => 3, ); - //Analytics settings. + //Analytics settings. $profile_id = $config->get('analytics.addthis_profile_id'); $can_track_clicks = empty($profile_id) ? FALSE : TRUE; $form['fieldset_analytics'] = array( @@ -233,9 +233,10 @@ public function buildForm(array $form, FormStateInterface $form_state) { ); // Google Analytics and Google Social Tracking support. - $can_do_google_social_tracking = \Drupal::moduleHandler()->moduleExists('googleanalytics'); + $can_do_google_social_tracking = \Drupal::moduleHandler() + ->moduleExists('googleanalytics'); //@TODO Get back to this. - $google_analytics_config = \Drupal::config(google_analytics.settings); + $google_analytics_config = \Drupal::config(google_analytics . settings); $google_analytics_account = $google_analytics_config->get('google_analytics_account'); $is_google_analytics_setup = $can_do_google_social_tracking && isset($google_analytics_account); $form['fieldset_analytics']['google_analytics'] = array( diff --git a/src/Plugin/Field/FieldFormatter/AddThisFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisFormatter.php index e73d6a9..08e32ef 100644 --- a/src/Plugin/Field/FieldFormatter/AddThisFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AddThisFormatter.php @@ -1,8 +1,8 @@ array(), ); diff --git a/src/Plugin/Field/FieldType/AddThisItem.php b/src/Plugin/Field/FieldType/AddThisItem.php index 536df80..5d9d06b 100644 --- a/src/Plugin/Field/FieldType/AddThisItem.php +++ b/src/Plugin/Field/FieldType/AddThisItem.php @@ -66,12 +66,17 @@ public function getConstraints() { $constraints = parent::getConstraints(); if ($max_length = $this->getSetting('max_length')) { - $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager(); + $constraint_manager = \Drupal::typedDataManager() + ->getValidationConstraintManager(); $constraints[] = $constraint_manager->create('ComplexData', array( 'value' => array( 'Length' => array( 'max' => $max_length, - 'maxMessage' => t('%name: may not be longer than @max characters.', array('%name' => $this->getFieldDefinition()->getLabel(), '@max' => $max_length)), + 'maxMessage' => t('%name: may not be longer than @max characters.', array( + '%name' => $this->getFieldDefinition() + ->getLabel(), + '@max' => $max_length + )), ), ), )); diff --git a/src/Plugin/Field/FieldWidget/AddThisWidget.php b/src/Plugin/Field/FieldWidget/AddThisWidget.php index 7041f29..ada3a4f 100644 --- a/src/Plugin/Field/FieldWidget/AddThisWidget.php +++ b/src/Plugin/Field/FieldWidget/AddThisWidget.php @@ -29,8 +29,7 @@ class AddThisWidget extends WidgetBase { * {@inheritdoc} */ public static function defaultSettings() { - return array( - ) + parent::defaultSettings(); + return array() + parent::defaultSettings(); } /** diff --git a/src/Services/AddThisScriptManager.php b/src/Services/AddThisScriptManager.php index 4c2834d..560b807 100644 --- a/src/Services/AddThisScriptManager.php +++ b/src/Services/AddThisScriptManager.php @@ -27,8 +27,10 @@ class AddThisScriptManager { * Construct method. */ private function __construct() { - $this->async = \Drupal::config('addthis.settings.advanced')->get('addthis_widget_load_async'); - $this->domready = \Drupal::config('addthis.settings.advanced')->get('addthis_widget_load_domready'); + $this->async = \Drupal::config('addthis.settings.advanced') + ->get('addthis_widget_load_async'); + $this->domready = \Drupal::config('addthis.settings.advanced') + ->get('addthis_widget_load_domready'); } /** @@ -52,7 +54,8 @@ public static function getInstance() { * A url reference to the widget js. */ public function getWidgetJsUrl() { - return check_url(\Drupal::config('addthis.settings.advanced')->get('addthis_widget_js_url')); + return check_url(\Drupal::config('addthis.settings.advanced') + ->get('addthis_widget_js_url')); } /** @@ -79,7 +82,7 @@ public function isHttps() { public function correctSchemaIfHttps($url) { if (is_string($url) && $this->isHttps()) { return str_replace('http://', 'https://', $url); - } + } else { return $url; } @@ -192,12 +195,13 @@ private function getJsAddThisConfig() { ); if (\Drupal::moduleHandler()->moduleExists('googleanalytics')) { if ($config->get('analytics.addthis_google_analytics_tracking_enabled')) { - $configuration['data_ga_property'] = \Drupal::config(google_analytics.settings)->get('google_analytics_account'); + $configuration['data_ga_property'] = \Drupal::config(google_analytics . settings) + ->get('google_analytics_account'); $configuration['data_ga_social'] = $config->get('analytics.addthis_google_analytics_social_tracking_enabled'); } } - // drupal_alter('addthis_configuration', $configuration); + // drupal_alter('addthis_configuration', $configuration); return $configuration; } @@ -220,7 +224,8 @@ private function getJsAddThisShare() { 'templates' => $configuration['templates'], ); } - $addthis_share['templates']['twitter'] = \Drupal::config('addthis.settings')->get('third_party.addthis_twitter_template'); + $addthis_share['templates']['twitter'] = \Drupal::config('addthis.settings') + ->get('third_party.addthis_twitter_template'); //drupal_alter('addthis_configuration_share', $configuration); return $addthis_share; diff --git a/src/Util/AddThisJson.php b/src/Util/AddThisJson.php index ebe5c3e..da9bfc9 100644 --- a/src/Util/AddThisJson.php +++ b/src/Util/AddThisJson.php @@ -18,7 +18,8 @@ public function decode($url) { $response = \Drupal::httpClient() ->get($url) ->getBody(TRUE); - } catch (RequestException $e) { + } + catch (RequestException $e) { // Do some stuff in case of the error. } From 85f38b1bc6f824ccb5848e986910816dbffd5d7e Mon Sep 17 00:00:00 2001 From: Jason Enter Date: Thu, 13 Aug 2015 17:14:39 -0400 Subject: [PATCH 28/77] AddThis Hover Menu Broken #9 --- src/Services/AddThisScriptManager.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Services/AddThisScriptManager.php b/src/Services/AddThisScriptManager.php index 560b807..7d099e7 100644 --- a/src/Services/AddThisScriptManager.php +++ b/src/Services/AddThisScriptManager.php @@ -171,6 +171,8 @@ function setAsync($enabled) { * SRP is lost here. */ private function getJsAddThisConfig() { + + // TODO: find out why $language is returning null global $language; $config = \Drupal::config('addthis.settings'); @@ -191,7 +193,7 @@ private function getJsAddThisConfig() { 'ui_open_windows' => $config->get('compact_menu.menu_style.addthis_open_windows_enabled'), 'ui_use_css' => $config->get('compact_menu.additionals.addthis_standard_css_enabled'), 'ui_use_addressbook' => $config->get('compact_menu.additionals.addthis_addressbook_enabled'), - 'ui_language' => $language->language, + 'ui_language' => isset($language) ? $language->language : "und", ); if (\Drupal::moduleHandler()->moduleExists('googleanalytics')) { if ($config->get('analytics.addthis_google_analytics_tracking_enabled')) { From 0e1ec4f38042c7b9a4e4c59ef5578f8bd43fe2b2 Mon Sep 17 00:00:00 2001 From: Jason Enter Date: Fri, 14 Aug 2015 09:25:42 -0400 Subject: [PATCH 29/77] fix order of styles --- addthis.libraries.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/addthis.libraries.yml b/addthis.libraries.yml index fa93d09..fa5b978 100644 --- a/addthis.libraries.yml +++ b/addthis.libraries.yml @@ -2,9 +2,9 @@ addthis.admin: version: VERSION css: theme: - css/addthis.admin.css: {} http://cache.addthiscdn.com/icons/v1/sprites/services.css: { type: external } - + css/addthis.admin.css: {} + addthis.global: version: VERSION js: From fadaaab81c5608893b46ce626bb4ddca197d355a Mon Sep 17 00:00:00 2001 From: Jason Enter Date: Fri, 14 Aug 2015 10:31:47 -0400 Subject: [PATCH 30/77] updating markup renderable vars --- src/Element/AddThisWrapper.php | 10 ++++------ templates/addthis-wrapper.html.twig | 6 +++--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Element/AddThisWrapper.php b/src/Element/AddThisWrapper.php index dfb6d90..0141a33 100644 --- a/src/Element/AddThisWrapper.php +++ b/src/Element/AddThisWrapper.php @@ -37,18 +37,16 @@ public function getInfo() { * @return mixed */ public static function preRenderAddThisWrapper($element) { - $output = '<' . $element['#tag'] . new Attribute($element['#attributes']) . '>'; + $tag_open = '<' . $element['#tag'] . new Attribute($element['#attributes']) . '>'; $children = Element::children($element); + $output = ''; if (count($children) > 0) { foreach ($children as $child) { $output .= render($element[$child]); } } - $output .= ' \n"; - $element['addthis_wrapper'] = [ - '#markup' => $output - ]; - + $tag_close = ' \n"; + $element['#markup'] = $tag_open . $output . $tag_close; return $element; } diff --git a/templates/addthis-wrapper.html.twig b/templates/addthis-wrapper.html.twig index 2732157..696698b 100644 --- a/templates/addthis-wrapper.html.twig +++ b/templates/addthis-wrapper.html.twig @@ -3,10 +3,10 @@ * @file * Default theme implementation to display a node. * Available variables: - * - element: AddThis Wrapper to be rendered - * - element['']: Something else to render. + * - element: original AddThis wrapper object to reference + * - element['#markup']: Html markup to render for AddThis output * */ #} -{{ element.addthis_wrapper }} \ No newline at end of file +{{ element['#markup'] }} \ No newline at end of file From 7eaba71991eb73b92d0fad6510b482cbca9b1b01 Mon Sep 17 00:00:00 2001 From: Jason Enter Date: Fri, 14 Aug 2015 11:21:15 -0400 Subject: [PATCH 31/77] lang update --- src/Services/AddThisScriptManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Services/AddThisScriptManager.php b/src/Services/AddThisScriptManager.php index 7d099e7..52a4990 100644 --- a/src/Services/AddThisScriptManager.php +++ b/src/Services/AddThisScriptManager.php @@ -193,7 +193,7 @@ private function getJsAddThisConfig() { 'ui_open_windows' => $config->get('compact_menu.menu_style.addthis_open_windows_enabled'), 'ui_use_css' => $config->get('compact_menu.additionals.addthis_standard_css_enabled'), 'ui_use_addressbook' => $config->get('compact_menu.additionals.addthis_addressbook_enabled'), - 'ui_language' => isset($language) ? $language->language : "und", + 'ui_language' => isset($language) ? $language->language : LANGCODE_NOT_SPECIFIED, ); if (\Drupal::moduleHandler()->moduleExists('googleanalytics')) { if ($config->get('analytics.addthis_google_analytics_tracking_enabled')) { From 1ae8d8fa0fbcd0a7a0f5fa4a42019e54abea667a Mon Sep 17 00:00:00 2001 From: Michael Sherron Date: Fri, 14 Aug 2015 11:59:02 -0700 Subject: [PATCH 32/77] changing fieldsets to details, per new d8 form api --- src/Form/AddThisSettingsAdvancedForm.php | 32 ++++--- src/Form/AddThisSettingsForm.php | 110 +++++++++++------------ 2 files changed, 69 insertions(+), 73 deletions(-) diff --git a/src/Form/AddThisSettingsAdvancedForm.php b/src/Form/AddThisSettingsAdvancedForm.php index 5a6db82..3581755 100644 --- a/src/Form/AddThisSettingsAdvancedForm.php +++ b/src/Form/AddThisSettingsAdvancedForm.php @@ -41,31 +41,30 @@ public function buildForm(array $form, FormStateInterface $form_state) { $form['#attached']['library'][] = 'addthis/addthis.admin'; // Service URL's settings. - $form['service_urls_fieldset'] = array( - '#type' => 'fieldset', + $form['service_urls_details'] = array( + '#type' => 'details', '#title' => t('Service URLs'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, + '#open' => TRUE, ); - $form['service_urls_fieldset']['addthis_bookmark_url'] = array( + $form['service_urls_details']['addthis_bookmark_url'] = array( '#type' => 'textfield', '#title' => t('AddThis bookmark URL'), '#default_value' => $config->get('addthis_bookmark_url'), '#required' => TRUE, ); - $form['service_urls_fieldset']['addthis_services_css_url'] = array( + $form['service_urls_details']['addthis_services_css_url'] = array( '#type' => 'textfield', '#title' => t('AddThis services stylesheet URL'), '#default_value' => $config->get('addthis_services_css_url'), '#required' => TRUE, ); - $form['service_urls_fieldset']['addthis_services_json_url'] = array( + $form['service_urls_details']['addthis_services_json_url'] = array( '#type' => 'textfield', '#title' => t('AddThis services json URL'), '#default_value' => $config->get('addthis_services_json_url'), '#required' => TRUE, ); - $form['service_urls_fieldset']['addthis_widget_js_url'] = array( + $form['service_urls_details']['addthis_widget_js_url'] = array( '#type' => 'textfield', '#title' => t('AddThis javascript widget URL'), '#default_value' => $config->get('addthis_widget_js_url'), @@ -73,40 +72,39 @@ public function buildForm(array $form, FormStateInterface $form_state) { ); // Advanced settings. - $form['advanced_settings_fieldset'] = array( - '#type' => 'fieldset', + $form['advanced_settings_details'] = array( + '#type' => 'details', '#title' => t('Advanced settings'), '#access' => \Drupal::currentUser() ->hasPermission('administer advanced addthis'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, + '#open' => TRUE, ); - $form['advanced_settings_fieldset']['addthis_custom_configuration_code_enabled'] = array( + $form['advanced_settings_details']['addthis_custom_configuration_code_enabled'] = array( '#type' => 'checkbox', '#title' => t('Use custom AddThis configuration code'), '#default_value' => $config->get('addthis_custom_configuration_code_enabled'), '#required' => FALSE, '#description' => t('Use custom AddThis configuration code. If checked, custom configuration will be used instead of other configuration settings provided in AddThis administration user interface.'), ); - $form['advanced_settings_fieldset']['addthis_custom_configuration_code'] = array( + $form['advanced_settings_details']['addthis_custom_configuration_code'] = array( '#type' => 'textarea', '#title' => t('AddThis custom configuration code'), '#default_value' => $config->get('addthis_custom_configuration_code'), '#required' => FALSE, '#description' => t('AddThis custom configuration code. See format at AddThis.com'), ); - $form['advanced_settings_fieldset']['addthis_widget_load_domready'] = array( + $form['advanced_settings_details']['addthis_widget_load_domready'] = array( '#type' => 'checkbox', '#title' => t('Load the AddThis resources after the DOM is ready.'), '#default_value' => $config->get('addthis_widget_load_domready'), ); - $form['advanced_settings_fieldset']['addthis_widget_load_async'] = array( + $form['advanced_settings_details']['addthis_widget_load_async'] = array( '#type' => 'checkbox', '#title' => t('Initialize asynchronously through addthis.init().'), '#description' => t('Use this when you have your own Ajax functionality or create things after the DOM is ready trough Javascript. Initialize the addthis functionality through addthis.init().'), '#default_value' => $config->get('addthis_widget_load_async'), ); - $form['advanced_settings_fieldset']['addthis_widget_include'] = array( + $form['advanced_settings_details']['addthis_widget_include'] = array( '#type' => 'select', '#title' => t('Load widget js.'), '#options' => array( diff --git a/src/Form/AddThisSettingsForm.php b/src/Form/AddThisSettingsForm.php index 6cf4618..e90e055 100644 --- a/src/Form/AddThisSettingsForm.php +++ b/src/Form/AddThisSettingsForm.php @@ -11,6 +11,7 @@ use Drupal\Core\Extension\ModuleHandler; use Drupal\Component\Utility\Xss; use Drupal\addthis\AddThis; +use Symfony\Component\Validator\Constraints\False; /** * Defines a form to configure maintenance settings for this site. @@ -41,20 +42,19 @@ public function buildForm(array $form, FormStateInterface $form_state) { $form['#attached']['library'][] = 'addthis/addthis.admin'; // Visual settings. - $form['fieldset_compact_menu'] = array( - '#type' => 'fieldset', + $form['details_compact_menu'] = array( + '#type' => 'details', '#title' => t('Compact menu'), - '#collapsible' => FALSE, - '#collapsed' => TRUE, - '#description' => '

' . t('Configure the global behavior and style of the compact menu and some additional settings related to the interface.') . '

' + '#open' => TRUE, + '#description' => '

' . t('Configure the global behavior and style of the compact menu and some additional settings related to the interface.') . '

', + '#description_display' => 'before' ); - $form['fieldset_compact_menu']['fieldset_menu_style'] = array( - '#type' => 'fieldset', + $form['details_compact_menu']['details_menu_style'] = array( + '#type' => 'details', '#title' => t('Style'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, + '#open' => FALSE, ); - $form['fieldset_compact_menu']['fieldset_menu_style']['addthis_co_brand'] = array( + $form['details_compact_menu']['details_menu_style']['addthis_co_brand'] = array( '#type' => 'textfield', '#title' => t('Branding text'), '#description' => t('Additional branding message to be rendered in the upper-right-hand corner of the compact menus.
Should be less than 15 characters in most cases to render properly.'), @@ -62,39 +62,39 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#required' => FALSE, '#maxlength' => 15, ); - $form['fieldset_compact_menu']['fieldset_menu_style']['addthis_ui_header_color'] = array( + $form['details_compact_menu']['details_menu_style']['addthis_ui_header_color'] = array( '#type' => 'textfield', '#title' => t('Header text color'), '#default_value' => $config->get('compact_menu.menu_style.addthis_ui_header_color'), - '#description' => t('Something like #FFFFFF'), + '#description' => t('Something like \'#FFFFFF\'.'), '#size' => 8, '#maxlength' => 7, '#required' => FALSE, ); - $form['fieldset_compact_menu']['fieldset_menu_style']['addthis_ui_header_background_color'] = array( + $form['details_compact_menu']['details_menu_style']['addthis_ui_header_background_color'] = array( '#type' => 'textfield', '#title' => t('Header background color'), '#default_value' => $config->get('compact_menu.menu_style.addthis_ui_header_background_color'), - '#description' => t('Something like #000000'), + '#description' => t('Something like \'#000000\'.'), '#size' => 8, '#maxlength' => 7, '#required' => FALSE, ); - $form['fieldset_compact_menu']['fieldset_menu_style']['addthis_click_to_open_compact_menu_enabled'] = array( + $form['details_compact_menu']['details_menu_style']['addthis_click_to_open_compact_menu_enabled'] = array( '#type' => 'checkbox', '#title' => t('Open compact menu on click'), '#description' => t('Default behavior is to open compact menu on hover.'), '#default_value' => $config->get('compact_menu.menu_style.addthis_click_to_open_compact_menu_enabled'), '#required' => FALSE, ); - $form['fieldset_compact_menu']['fieldset_menu_style']['addthis_open_windows_enabled'] = array( + $form['details_compact_menu']['details_menu_style']['addthis_open_windows_enabled'] = array( '#type' => 'checkbox', '#title' => t('Use pop-up windows'), '#description' => t('If checked, all shares will open in a new pop-up window instead of a new tab or regular browser window.'), '#default_value' => $config->get('compact_menu.menu_style.addthis_open_windows_enabled'), '#required' => FALSE, ); - $form['fieldset_compact_menu']['fieldset_menu_style']['addthis_ui_delay'] = array( + $form['details_compact_menu']['details_menu_style']['addthis_ui_delay'] = array( '#type' => 'textfield', '#title' => t('Menu open delay'), '#description' => t('Delay, in milliseconds, before compact menu appears when mousing over a regular button. Capped at 500 ms.'), @@ -105,14 +105,14 @@ public function buildForm(array $form, FormStateInterface $form_state) { ); // Enabled services settings. - $form['fieldset_compact_menu']['enabled_services_fieldset'] = array( - '#type' => 'fieldset', + $form['details_compact_menu']['enabled_services_details'] = array( + '#type' => 'details', '#title' => t('Compact menu enabled services'), '#description' => t('The sharing services you select here will be displayed in the compact menu. If you select no services, AddThis will provide a list of frequently used services. This list is updated regularly. Notice that this setting does not define what services should be display in a toolbox.'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, + '#description_display' => 'before', + '#open' => FALSE, ); - $form['fieldset_compact_menu']['enabled_services_fieldset']['addthis_enabled_services'] = array( + $form['details_compact_menu']['enabled_services_details']['addthis_enabled_services'] = array( '#type' => 'checkboxes', '#title' => t('Enabled services'), '#options' => AddThis::getInstance()->getServices(), @@ -122,27 +122,26 @@ public function buildForm(array $form, FormStateInterface $form_state) { ); // Additional visual settings. - $form['fieldset_compact_menu']['fieldset_additionals'] = array( - '#type' => 'fieldset', + $form['details_compact_menu']['details_additionals'] = array( + '#type' => 'details', '#title' => t('Additional configuration'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, + '#open' => FALSE, ); - $form['fieldset_compact_menu']['fieldset_additionals']['addthis_standard_css_enabled'] = array( + $form['details_compact_menu']['details_additionals']['addthis_standard_css_enabled'] = array( '#type' => 'checkbox', '#title' => t('Use standard AddThis stylesheet'), '#description' => t('If not checked, AddThis will not load standard CSS file, allowing you to style everything yourself without incurring the cost of an additional load.'), '#default_value' => $config->get('compact_menu.additionals.addthis_standard_css_enabled'), '#required' => FALSE, ); - $form['fieldset_compact_menu']['fieldset_additionals']['addthis_508_compliant'] = array( + $form['details_compact_menu']['details_additionals']['addthis_508_compliant'] = array( '#type' => 'checkbox', '#title' => t('508 compliant'), '#description' => 'If checked, clicking the AddThis button will open a new window to a page that is keyboard navigable.', '#default_value' => $config->get('compact_menu.additionals.addthis_508_compliant'), '#required' => FALSE, ); - $form['fieldset_compact_menu']['fieldset_additionals']['addthis_addressbook_enabled'] = array( + $form['details_compact_menu']['details_additionals']['addthis_addressbook_enabled'] = array( '#type' => 'checkbox', '#title' => t('Use addressbook'), '#description' => 'If checked, the user will be able import their contacts from popular webmail services when using AddThis\'s email sharing.', @@ -151,14 +150,14 @@ public function buildForm(array $form, FormStateInterface $form_state) { ); // Excluded Services. - $form['fieldset_excluded_services'] = array( - '#type' => 'fieldset', + $form['details_excluded_services'] = array( + '#type' => 'details', '#title' => t('Excluded services'), '#description' => t('The sharing services you select here will be excluded from all AddThis menus. This applies globally.'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, + '#description_display' => 'before', + '#open' => FALSE, ); - $form['fieldset_excluded_services']['addthis_excluded_services'] = array( + $form['details_excluded_services']['addthis_excluded_services'] = array( '#type' => 'checkboxes', '#title' => t('Excluded services'), '#options' => AddThis::getInstance()->getServices(), @@ -170,22 +169,22 @@ public function buildForm(array $form, FormStateInterface $form_state) { //Analytics settings. $profile_id = $config->get('analytics.addthis_profile_id'); $can_track_clicks = empty($profile_id) ? FALSE : TRUE; - $form['fieldset_analytics'] = array( - '#type' => 'fieldset', + $form['details_analytics'] = array( + '#type' => 'details', '#title' => t('Analytics and Tracking'), - '#collapsible' => TRUE, - '#collapsed' => $can_track_clicks ? TRUE : FALSE, + '#open' => $can_track_clicks ? TRUE : FALSE, ); if (!$can_track_clicks) { - $form['fieldset_analytics']['can_track_notice'] = array( + $form['details_analytics']['can_track_notice'] = array( '#theme' => 'html_tag', '#tag' => 'div', '#value' => t('For click analytics and statistics you have to provide a ProfileID from AddThis.com. Register here.'), '#attributes' => array('class' => array('messages', 'warning')), ); } - $form['fieldset_analytics']['addthis_profile_id'] = array( + + $form['details_analytics']['addthis_profile_id'] = array( '#type' => 'textfield', '#title' => t('AddThis ProfileID'), '#default_value' => $config->get('analytics.addthis_profile_id'), @@ -193,7 +192,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#size' => 25, '#description' => t('ProfileID at AddThis.com. Required for statistics.'), ); - $form['fieldset_analytics']['addthis_clickback_tracking_enabled'] = array( + $form['details_analytics']['addthis_clickback_tracking_enabled'] = array( '#type' => 'checkbox', '#title' => t('Track clickback'), '#description' => 'Check to allow AddThis to append a variable to your URLs upon sharing. AddThis will use this to track how many people come back to your content via links shared with AddThis. Highly recommended. Always global.', @@ -211,19 +210,19 @@ public function buildForm(array $form, FormStateInterface $form_state) { else { $rdf_description = t('Check to enable Facebook Like tracking support. Always global.'); } - $form['fieldset_analytics']['title_facebook'] = array( + $form['details_analytics']['title_facebook'] = array( '#theme' => 'html_tag', '#tag' => 'div', '#value' => '' . t('Facebook') . '', ); - $form['fieldset_analytics']['facebook_notice'] = array( + $form['details_analytics']['facebook_notice'] = array( '#theme' => 'html_tag', '#tag' => 'p', '#value' => $rdf_description, '#access' => !$rdf_enabled, ); - $form['fieldset_analytics']['addthis_facebook_like_count_support_enabled'] = array( + $form['details_analytics']['addthis_facebook_like_count_support_enabled'] = array( '#type' => 'checkbox', '#title' => t('Enable Facebook Like tracking'), '#description' => Xss::filter($rdf_description, array('span')), @@ -239,26 +238,26 @@ public function buildForm(array $form, FormStateInterface $form_state) { $google_analytics_config = \Drupal::config(google_analytics . settings); $google_analytics_account = $google_analytics_config->get('google_analytics_account'); $is_google_analytics_setup = $can_do_google_social_tracking && isset($google_analytics_account); - $form['fieldset_analytics']['google_analytics'] = array( + $form['details_analytics']['google_analytics'] = array( '#theme' => 'html_tag', '#tag' => 'div', '#value' => '' . t('Google Analytics') . '', ); if (!$can_do_google_social_tracking) { - $form['fieldset_analytics']['can_do_google_analytics'] = array( + $form['details_analytics']['can_do_google_analytics'] = array( '#theme' => 'html_tag', '#tag' => 'p', '#value' => '' . t('Install/enable the Google Analytics module for Social Tracking support.') . '', ); } elseif ($can_do_google_social_tracking && !$is_google_analytics_setup) { - $form['fieldset_analytics']['can_do_google_analytics'] = array( + $form['details_analytics']['can_do_google_analytics'] = array( '#theme' => 'html_tag', '#tag' => 'p', '#value' => '' . t('Configure the Google Analytics module correctly with the account code to use this feature.') . '', ); } - $form['fieldset_analytics']['addthis_google_analytics_tracking_enabled'] = array( + $form['details_analytics']['addthis_google_analytics_tracking_enabled'] = array( '#type' => 'checkbox', '#title' => t('Track with Google Analytics'), '#description' => t('Check to track shares in your Google Analytics account reports (more info). Always global.'), @@ -266,7 +265,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#required' => FALSE, '#disabled' => !$is_google_analytics_setup, ); - $form['fieldset_analytics']['addthis_google_analytics_social_tracking_enabled'] = array( + $form['details_analytics']['addthis_google_analytics_social_tracking_enabled'] = array( '#type' => 'checkbox', '#title' => t('Track with Google Analytics social'), '#description' => t('Check to track shares in the new Google Analytics social interaction reports (more info). Always global.'), @@ -276,26 +275,25 @@ public function buildForm(array $form, FormStateInterface $form_state) { ); // Third party settings. - $form['third_party_fieldset'] = array( - '#type' => 'fieldset', + $form['third_party_details'] = array( + '#type' => 'details', '#title' => t('Third party settings'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, + '#open' => FALSE, ); - $form['third_party_fieldset']['twitter_service'] = array( + $form['third_party_details']['twitter_service'] = array( '#type' => 'fieldset', '#title' => t('Twitter'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); - $form['third_party_fieldset']['twitter_service']['addthis_twitter_via'] = array( + $form['third_party_details']['twitter_service']['addthis_twitter_via'] = array( '#type' => 'textfield', '#title' => t('Send via'), '#description' => t('When sending a tweet this is the screen name of the user to attribute the Tweet to. (Relates to Tweet Button)'), '#default_value' => $config->get('third_party.addthis_twitter_via'), '#size' => 15, ); - $form['third_party_fieldset']['twitter_service']['addthis_twitter_template'] = array( + $form['third_party_details']['twitter_service']['addthis_twitter_template'] = array( '#type' => 'textfield', '#title' => t('Template text'), '#description' => t('The {{title}} and {{url}} are replaced from the Twitter Button.'), From ba167eac597b94cd171ec903d5b869bc599dcf7c Mon Sep 17 00:00:00 2001 From: Doyle Date: Mon, 17 Aug 2015 10:08:17 -0400 Subject: [PATCH 33/77] Changed README.txt to README.md --- README.txt => README.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename README.txt => README.md (100%) diff --git a/README.txt b/README.md similarity index 100% rename from README.txt rename to README.md From 08da971dba6ba7e7e296bfffa21c1e33a5aa8b90 Mon Sep 17 00:00:00 2001 From: Daniel Montgomery Date: Mon, 17 Aug 2015 12:57:03 -0500 Subject: [PATCH 34/77] - Refactored services to use the services.yml pattern. - Most testing is done with the block system. - Started refactoring the element types to use one twig template per. - The javascript doesn't work currently and will be the next thing to look at. --- addthis.module | 18 ++-- addthis.services.yml | 10 +++ .../src/Plugin/Block/AddThisBlock.php | 33 ++++--- .../AddThisBasicButtonFormatter.php | 6 +- .../AddThisBasicToolboxFormatter.php | 7 +- config/install/addthis.settings.advanced.yml | 1 + config/install/addthis.settings.yml | 1 + src/AddThis.php | 86 +++++-------------- src/{Services => }/AddThisScriptManager.php | 74 ++++++++-------- src/Element/AddThisBasicButton.php | 65 ++++++++++++++ src/Element/AddThisBasicToolbox.php | 28 ++++++ src/Element/AddThisElement.php | 35 -------- src/Element/AddThisWrapper.php | 24 ------ src/Form/AddThisSettingsForm.php | 7 +- templates/addthis-basic-button.html.twig | 12 +++ templates/addthis-basic-toolbox.html.twig | 10 +++ templates/addthis-element.html.twig | 8 +- templates/addthis-wrapper.html.twig | 6 +- 18 files changed, 236 insertions(+), 195 deletions(-) create mode 100644 addthis.services.yml rename src/{Services => }/AddThisScriptManager.php (76%) create mode 100644 src/Element/AddThisBasicButton.php create mode 100644 src/Element/AddThisBasicToolbox.php create mode 100644 templates/addthis-basic-button.html.twig create mode 100644 templates/addthis-basic-toolbox.html.twig diff --git a/addthis.module b/addthis.module index 0679aba..c8d823c 100644 --- a/addthis.module +++ b/addthis.module @@ -4,14 +4,20 @@ * Implements hook_theme(). */ function addthis_theme() { - return array( - 'addthis_wrapper' => array( + return [ + 'addthis_wrapper' => [ 'render element' => 'element', - ), - 'addthis_element' => array( + ], + 'addthis_element' => [ 'render element' => 'element', - ), - ); + ], + 'addthis_basic_button' => [ + 'render element' => 'element', + ], + 'addthis_basic_toolbox' => [ + 'render element' => 'element', + ], + ]; } /** diff --git a/addthis.services.yml b/addthis.services.yml new file mode 100644 index 0000000..b0cea68 --- /dev/null +++ b/addthis.services.yml @@ -0,0 +1,10 @@ +services: + # AddThis service? + addthis.addthis: + class: Drupal\addthis\AddThis + arguments: ['@addthis.script_manager', '@config.factory'] + + # This service creates a more complex access check. + addthis.script_manager: + class: Drupal\addthis\AddThisScriptManager + arguments: ['@language_manager', '@config.factory'] diff --git a/addthis_block/src/Plugin/Block/AddThisBlock.php b/addthis_block/src/Plugin/Block/AddThisBlock.php index 777e423..a7c8228 100644 --- a/addthis_block/src/Plugin/Block/AddThisBlock.php +++ b/addthis_block/src/Plugin/Block/AddThisBlock.php @@ -46,7 +46,8 @@ public function defaultConfiguration() { function blockForm($form, FormStateInterface $form_state) { // The list of formatters. - $formatter_options = AddThis::getInstance()->getDisplayTypes(); + $add_this_service = \Drupal::service('addthis.addthis'); + $formatter_options = $add_this_service->getDisplayTypes(); $settings = $this->getConfiguration(); $type = $settings['type']; @@ -88,13 +89,13 @@ function blockForm($form, FormStateInterface $form_state) { '#suffix' => '', ); if ($type == 'addthis_basic_toolbox') { - $basicToolbox = AddThis::getInstance() + $basicToolbox = $add_this_service ->getBasicToolboxForm($this, $settings['basic_toolbox']); $form['settings']['addthis_settings']['type_settings']['basic_toolbox'] = $basicToolbox; } else { if ($type == 'addthis_basic_button') { - $basicButton = AddThis::getInstance() + $basicButton = $add_this_service ->getBasicButtonForm($this, $settings['basic_button']); $form['settings']['addthis_settings']['type_settings']['basic_button'] = $basicButton; } @@ -173,27 +174,23 @@ public function blockSubmit($form, FormStateInterface $form_state) { */ public function build() { $config = $this->configuration; + switch ($config['type']) { case 'addthis_basic_button': - $markup = AddThis::getInstance() - ->getBasicButtonMarkup($config['basic_button']); + return [ + '#type' => 'addthis_basic_button', + '#size' => $config['basic_button']['button_size'], + ]; break; case 'addthis_basic_toolbox': - $markup = AddThis::getInstance() - ->getBasicToolboxMarkup($config['basic_toolbox']); - break; - default: - $markup = ''; + return [ + '#type' => 'addthis_basic_toolbox', + ]; break; } - return array( - '#markup' => $markup - ); - + return [ + '#markup' => '' + ]; } - - } - -?> \ No newline at end of file diff --git a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php index c1d5160..e557b64 100644 --- a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php +++ b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php @@ -41,7 +41,8 @@ public static function defaultSettings() { public function settingsForm(array $form, FormStateInterface $form_state) { $settings = $this->getSettings(); - $element = AddThis::getInstance()->getBasicButtonForm($this, $settings); + $add_this_service = \Drupal::service('addthis.addthis'); + $element = $add_this_service->getBasicButtonForm($this, $settings); return $element; } @@ -52,7 +53,8 @@ public function settingsForm(array $form, FormStateInterface $form_state) { */ public function viewElements(FieldItemListInterface $items) { $settings = $this->getSettings(); - $markup = AddThis::getInstance()->getBasicButtonMarkup($settings); + $add_this_service = \Drupal::service('addthis.addthis'); + $markup = $add_this_service->getBasicButtonMarkup($settings); return array( '#markup' => $markup ); diff --git a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php index d437e9c..1a78fae 100644 --- a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php +++ b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php @@ -43,8 +43,8 @@ public function settingsForm(array $form, FormStateInterface $form_state) { $settings = $this->getSettings(); $element = array(); - - AddThis::getInstance()->getBasicToolboxForm($this, $settings); + $add_this_service = \Drupal::service('addthis.addthis'); + $add_this_service->getBasicToolboxForm($this, $settings); return $element; } @@ -56,7 +56,8 @@ public function settingsForm(array $form, FormStateInterface $form_state) { public function viewElements(FieldItemListInterface $items) { $settings = $this->getSettings(); - $markup = AddThis::getInstance()->getBasicToolboxMarkup($settings); + $add_this_service = \Drupal::service('addthis.addthis'); + $markup = $add_this_service->getBasicToolboxMarkup($settings); return array( '#markup' => $markup diff --git a/config/install/addthis.settings.advanced.yml b/config/install/addthis.settings.advanced.yml index 2da0536..b1cd51c 100644 --- a/config/install/addthis.settings.advanced.yml +++ b/config/install/addthis.settings.advanced.yml @@ -1,3 +1,4 @@ +# TODO: remove prefix from these variables. Combine with addthis.settings.yml? addthis_bookmark_url: 'http://www.addthis.com/bookmark.php?v=300' addthis_services_css_url: 'http://cache.addthiscdn.com/icons/v1/sprites/services.css' addthis_services_json_url: 'http://cache.addthiscdn.com/services/v1/sharing.en.json' diff --git a/config/install/addthis.settings.yml b/config/install/addthis.settings.yml index 33a16e4..05904de 100644 --- a/config/install/addthis.settings.yml +++ b/config/install/addthis.settings.yml @@ -1,3 +1,4 @@ +#TODO: remove prefix from variables? compact_menu: menu_style: addthis_co_brand: '' diff --git a/src/AddThis.php b/src/AddThis.php index 6981a2e..8b29d5d 100644 --- a/src/AddThis.php +++ b/src/AddThis.php @@ -1,6 +1,9 @@ setJson(new AddThisJson()); - $add_this->setConfig(); - self::$instance = $add_this; - } + /** + * @var \Drupal\Core\Config\ConfigFactory + */ + protected $config_factory; - return self::$instance; + /** + * @param \Drupal\addthis\AddThisScriptManager $addThisScriptManager + */ + public function __construct(\Drupal\addthis\AddThisScriptManager $addThisScriptManager, \Drupal\Core\Config\ConfigFactory $configFactory){ + $this->add_this_script_manager = $addThisScriptManager; + $this->config_factory = $configFactory; + $this->json = new AddThisJson(); + $this->setConfig(); } /** @@ -101,7 +105,7 @@ public function setJson(AddThisJson $json) { } public function setConfig() { - $this->config = \Drupal::config('addthis.settings'); + $this->config = $this->config_factory->get('addthis.settings'); } @@ -359,7 +363,7 @@ function getBasicToolboxMarkup($settings) { ); // Add the widget script. - $script_manager = AddThisScriptManager::getInstance(); + $script_manager = \Drupal::service('addthis.script_manager'); $script_manager->attachJsToElement($element); @@ -374,9 +378,9 @@ function getBasicToolboxMarkup($settings) { $items[$service] = array( '#type' => 'addthis_element', '#tag' => 'a', - '#value' => '', + 'value' => '', '#attributes' => array( - 'href' => AddThis::getInstance()->getBaseBookmarkUrl(), + 'href' => $script_manager->getBaseBookmarkUrl(), 'class' => array( 'addthis_button_' . $service, ), @@ -418,7 +422,7 @@ function getBasicToolboxMarkup($settings) { case 'tweet': $items[$service]['#attributes'] += array( 'tw:count' => ($orientation ? 'horizontal' : 'vertical'), - 'tw:via' => AddThis::getInstance()->getTwitterVia(), + 'tw:via' => $script_manager->getTwitterVia(), ); break; case 'bubble_style': @@ -440,50 +444,4 @@ function getBasicToolboxMarkup($settings) { return render($element); } - - - /** - * Returns rendered markup for the BasicButton display. This will be called - * from both the Field and Block render functions. - * @param $settings - * @return null - */ - function getBasicButtonMarkup($settings) { - $button_img = 'http://s7.addthis.com/static/btn/sm-share-en.gif'; - if (isset($settings['buttons_size']) && $settings['buttons_size'] == 'big') { - $button_img = 'http://s7.addthis.com/static/btn/v2/lg-share-en.gif'; - } - //$button_img = $addthis->transformToSecureUrl($button_img); - - //$extra_css = isset($settings['extra_css']) ? $settings['extra_css'] : ''; - $element = array( - '#type' => 'addthis_wrapper', - '#tag' => 'a', - '#attributes' => array( - 'class' => array( - 'addthis_button', - ), - ), - ); - //$element['#attributes'] += $addthis->getAddThisAttributesMarkup($options); - - // Add the widget script. - $script_manager = AddThisScriptManager::getInstance(); - $script_manager->attachJsToElement($element); - - // Create img button. - $image = array( - '#type' => 'addthis_element', - '#tag' => 'img', - '#attributes' => array( - 'src' => $button_img, - 'alt' => t('Share page with AddThis'), - ), - ); - $element[] = $image; - - return render($element); - } - - } \ No newline at end of file diff --git a/src/Services/AddThisScriptManager.php b/src/AddThisScriptManager.php similarity index 76% rename from src/Services/AddThisScriptManager.php rename to src/AddThisScriptManager.php index 52a4990..deb642d 100644 --- a/src/Services/AddThisScriptManager.php +++ b/src/AddThisScriptManager.php @@ -1,6 +1,9 @@ async = \Drupal::config('addthis.settings.advanced') - ->get('addthis_widget_load_async'); - $this->domready = \Drupal::config('addthis.settings.advanced') - ->get('addthis_widget_load_domready'); - } + protected $language_manager; + + /** + * @var \Drupal\Core\Config\ConfigFactory + */ + protected $config_factory; /** - * Return a single instance of the AddThisScriptManager. + * Construct function. * - * @return AddThisScriptManager + * @param \Drupal\Core\Language\LanguageManager $languageManager + * @param \Drupal\Core\Config\ConfigFactory $configFactory */ - public static function getInstance() { - static $manager; + public function __construct(\Drupal\Core\Language\LanguageManager $languageManager, \Drupal\Core\Config\ConfigFactory $configFactory) { + $this->language_manager = $languageManager; + $this->config_factory = $configFactory; - if (!isset($manager)) { - $manager = new AddThisScriptManager(); - } - return $manager; + // TODO: Remove these from here. They can be referenced when we use them. + $config = $this->config_factory->get('addthis.settings.advanced'); + $this->async = $config->get('addthis_widget_load_async'); + $this->domready = $config->get('addthis_widget_load_domready'); } /** @@ -54,13 +59,14 @@ public static function getInstance() { * A url reference to the widget js. */ public function getWidgetJsUrl() { - return check_url(\Drupal::config('addthis.settings.advanced') - ->get('addthis_widget_js_url')); + // TODO: This uses a global function, why do we need to check the URL at this point? + return check_url($this->config_factory->get('addthis.settings.advanced')->get('addthis_widget_js_url')); } /** * Return if we are on https connection. * + * TODO: Why are we doing all of this URL processing? * @return bool * TRUE if the current request is on https. */ @@ -73,6 +79,7 @@ public function isHttps() { /** * Change the schema from http to https if we are on https. * + * TODO: Why are we doing all of this URL processing? * @param string $url * A full url. * @@ -101,11 +108,12 @@ public function correctSchemaIfHttps($url) { * The element to attach the JavaScript to. */ public function attachJsToElement(&$element) { - $config = \Drupal::config('addthis.settings'); - $adv_config = \Drupal::config('addthis.settings.advanced'); + $config = $this->config_factory->get('addthis.settings'); + $adv_config = $this->config_factory->get('addthis.settings.advanced'); if ($adv_config->get('addthis_widget_js_include') !== 0) { $widget_js = new AddThisWidgetJsUrl($adv_config->get('addthis_widget_js_url')); + $pubid = $config->get('analytics.addthis_profile_id'); if (isset($pubid) && !empty($pubid) && is_string($pubid)) { $widget_js->addAttribute('pubid', $pubid); @@ -116,14 +124,12 @@ public function attachJsToElement(&$element) { $widget_js->addAttribute('async', 1); } - $domready = $this->domready; - if ($domready) { + if ($this->domready) { $widget_js->addAttribute('domready', 1); } - - // Only when the script is not loaded after the DOM is ready we include - // the script with #attached. - if (!$domready) { + else { + // Only when the script is not loaded after the DOM is ready we include + // the script with #attached. $element['#attached']['library'][] = 'addthis/addthis.widget'; } @@ -132,7 +138,7 @@ public function attachJsToElement(&$element) { // passed here. $element['#attached']['drupalSettings']['addthis'] = array( 'async' => $async, - 'domready' => $domready, + 'domready' => $this->domready, 'widget_url' => $this->getWidgetJsUrl(), 'addthis_config' => $this->getJsAddThisConfig(), 'addthis_share' => $this->getJsAddThisShare(), @@ -171,10 +177,7 @@ function setAsync($enabled) { * SRP is lost here. */ private function getJsAddThisConfig() { - - // TODO: find out why $language is returning null - global $language; - $config = \Drupal::config('addthis.settings'); + $config = $this->config_factory->get('addthis.settings'); $enabled_services = $this->getServiceNamesAsCommaSeparatedString($config->get('compact_menu.enabled_services.addthis_enabled_services')) . 'more'; $excluded_services = $this->getServiceNamesAsCommaSeparatedString($config->get('excluded_services.addthis_excluded_services')); @@ -193,12 +196,13 @@ private function getJsAddThisConfig() { 'ui_open_windows' => $config->get('compact_menu.menu_style.addthis_open_windows_enabled'), 'ui_use_css' => $config->get('compact_menu.additionals.addthis_standard_css_enabled'), 'ui_use_addressbook' => $config->get('compact_menu.additionals.addthis_addressbook_enabled'), - 'ui_language' => isset($language) ? $language->language : LANGCODE_NOT_SPECIFIED, + // TODO: check that this returns what we want. + 'ui_language' => $this->language_manager->getCurrentLanguage(), ); + // TODO: Do we need to check if the module exists or can we just check the setting? if (\Drupal::moduleHandler()->moduleExists('googleanalytics')) { if ($config->get('analytics.addthis_google_analytics_tracking_enabled')) { - $configuration['data_ga_property'] = \Drupal::config(google_analytics . settings) - ->get('google_analytics_account'); + $configuration['data_ga_property'] = $this->config_factory->get('google_analytics.settings')->get('google_analytics_account'); $configuration['data_ga_social'] = $config->get('analytics.addthis_google_analytics_social_tracking_enabled'); } } @@ -226,7 +230,7 @@ private function getJsAddThisShare() { 'templates' => $configuration['templates'], ); } - $addthis_share['templates']['twitter'] = \Drupal::config('addthis.settings') + $addthis_share['templates']['twitter'] = $this->config_factory->get('addthis.settings') ->get('third_party.addthis_twitter_template'); //drupal_alter('addthis_configuration_share', $configuration); diff --git a/src/Element/AddThisBasicButton.php b/src/Element/AddThisBasicButton.php new file mode 100644 index 0000000..a866813 --- /dev/null +++ b/src/Element/AddThisBasicButton.php @@ -0,0 +1,65 @@ + 'addthis_basic_button', + '#size' => 'small', + '#pre_render' => [ + [$class, 'preRender'], + ], + ]; + } + + public function preRender($element) { + // Add button. + $button_img = 'http://s7.addthis.com/static/btn/sm-share-en.gif'; + if ($element['size'] === 'big') { + $button_img = 'http://s7.addthis.com/static/btn/v2/lg-share-en.gif'; + } + + // Create img button. + $element['image'] = [ + '#theme' => 'image', + '#uri' => $button_img, + '#alt' => t('Share page with AddThis'), + ]; + + // Add Script. + // TODO: Figure out what JS should be attached here. + $config_factory = \Drupal::getContainer()->get('config.factory'); + $config = $config_factory->get('addthis.settings'); + $adv_config = $config_factory->get('addthis.settings.advanced'); + + $element['#attached']['library'][] = 'addthis/addthis.widget'; + + /** + * Every setting value passed here overrides previously set values but + * leaves the values that are already set somewhere else and that are not + * passed here. + */ + $element['#attached']['drupalSettings']['addthis'] = []; + + return $element; + } + +} \ No newline at end of file diff --git a/src/Element/AddThisBasicToolbox.php b/src/Element/AddThisBasicToolbox.php new file mode 100644 index 0000000..20a05e4 --- /dev/null +++ b/src/Element/AddThisBasicToolbox.php @@ -0,0 +1,28 @@ + 'addthis_basic_toolbox', + ]; + } + +} \ No newline at end of file diff --git a/src/Element/AddThisElement.php b/src/Element/AddThisElement.php index e053d3c..8bda7dc 100644 --- a/src/Element/AddThisElement.php +++ b/src/Element/AddThisElement.php @@ -20,44 +20,9 @@ class AddThisElement extends RenderElement { * {@inheritdoc} */ public function getInfo() { - $class = get_class($this); return [ '#theme' => 'addthis_element', - '#pre_render' => [ - [$class, 'preRenderAddThisElement'], - ] ]; } - - /** - * Implements preRenderAddThisElement() - * - Defines consistent markup for new render type of addthis_element. - * @param $element - * @return mixed - */ - public static function preRenderAddThisElement($element) { - if (!isset($element['#value'])) { - $element['addthis_element'] = [ - '#markup' => '<' . $element['#tag'] . new Attribute($element['#attributes']) . " />\n" - ]; - return $element; - } - - $output = '<' . $element['#tag'] . new Attribute($element['#attributes']) . '>'; - if (isset($element['#value_prefix'])) { - $output .= $element['#value_prefix']; - } - $output .= $element['#value']; - if (isset($element['#value_suffix'])) { - $output .= $element['#value_suffix']; - } - $output .= '\n"; - $element['addthis_element'] = [ - '#markup' => $output, - ]; - - return $element; - } - } \ No newline at end of file diff --git a/src/Element/AddThisWrapper.php b/src/Element/AddThisWrapper.php index 0141a33..0963513 100644 --- a/src/Element/AddThisWrapper.php +++ b/src/Element/AddThisWrapper.php @@ -21,33 +21,9 @@ class AddThisWrapper extends RenderElement { * {@inheritdoc} */ public function getInfo() { - $class = get_class($this); return array( '#theme' => 'addthis_wrapper', - '#pre_render' => array( - array($class, 'preRenderAddThisWrapper'), - ) ); } - /** - * Implements preRenderAddThisWrapper() - * - Defines consistent markup for the addthis_wrapper render element. - * @param $element - * @return mixed - */ - public static function preRenderAddThisWrapper($element) { - $tag_open = '<' . $element['#tag'] . new Attribute($element['#attributes']) . '>'; - $children = Element::children($element); - $output = ''; - if (count($children) > 0) { - foreach ($children as $child) { - $output .= render($element[$child]); - } - } - $tag_close = ' \n"; - $element['#markup'] = $tag_open . $output . $tag_close; - return $element; - } - } \ No newline at end of file diff --git a/src/Form/AddThisSettingsForm.php b/src/Form/AddThisSettingsForm.php index e90e055..a48816e 100644 --- a/src/Form/AddThisSettingsForm.php +++ b/src/Form/AddThisSettingsForm.php @@ -112,10 +112,13 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#description_display' => 'before', '#open' => FALSE, ); + + $add_this_service = \Drupal::service('addthis.addthis'); + $form['details_compact_menu']['enabled_services_details']['addthis_enabled_services'] = array( '#type' => 'checkboxes', '#title' => t('Enabled services'), - '#options' => AddThis::getInstance()->getServices(), + '#options' => $add_this_service->getServices(), '#default_value' => $config->get('compact_menu.enabled_services.addthis_enabled_services'), '#required' => FALSE, '#columns' => 3, @@ -160,7 +163,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { $form['details_excluded_services']['addthis_excluded_services'] = array( '#type' => 'checkboxes', '#title' => t('Excluded services'), - '#options' => AddThis::getInstance()->getServices(), + '#options' => $add_this_service->getServices(), '#default_value' => $config->get('excluded_services.addthis_excluded_services'), '#required' => FALSE, '#columns' => 3, diff --git a/templates/addthis-basic-button.html.twig b/templates/addthis-basic-button.html.twig new file mode 100644 index 0000000..40e17d0 --- /dev/null +++ b/templates/addthis-basic-button.html.twig @@ -0,0 +1,12 @@ +{# +/** + * @file + * Default theme implementation to display a node.. + * Available variables: + * - element: AddThis Wrapper to be rendered + * + */ +#} + + {{ element }} + diff --git a/templates/addthis-basic-toolbox.html.twig b/templates/addthis-basic-toolbox.html.twig new file mode 100644 index 0000000..791390a --- /dev/null +++ b/templates/addthis-basic-toolbox.html.twig @@ -0,0 +1,10 @@ +{# +/** + * @file + * Default theme implementation to display a node.. + * Available variables: + * - element: AddThis Wrapper to be rendered + * + */ +#} +{{ element }} diff --git a/templates/addthis-element.html.twig b/templates/addthis-element.html.twig index 0abe9d9..2b25958 100644 --- a/templates/addthis-element.html.twig +++ b/templates/addthis-element.html.twig @@ -4,9 +4,11 @@ * Default theme implementation to display a node.. * Available variables: * - element: AddThis Wrapper to be rendered - * - element['']: Something else to render. * */ #} - -{{ element.addthis_element }} \ No newline at end of file +
addthis element
+{{ kint() }} +
+ {{ element }} +
diff --git a/templates/addthis-wrapper.html.twig b/templates/addthis-wrapper.html.twig index 696698b..9d49f0c 100644 --- a/templates/addthis-wrapper.html.twig +++ b/templates/addthis-wrapper.html.twig @@ -4,9 +4,9 @@ * Default theme implementation to display a node. * Available variables: * - element: original AddThis wrapper object to reference - * - element['#markup']: Html markup to render for AddThis output * */ #} - -{{ element['#markup'] }} \ No newline at end of file +
+ {{ element }} +
From 99b7c32e1f743a6ccfc0dbaab66c4b403b22b11e Mon Sep 17 00:00:00 2001 From: Daniel Montgomery Date: Mon, 17 Aug 2015 14:34:17 -0500 Subject: [PATCH 35/77] overhaul the Javascript Remove most of the JS for now and load the simplest case. We may need to do the complex logic, but it wasn't clear what it was for. Simplified AddThisScriptManager service for now to provide access to aggregated settings. --- addthis.libraries.yml | 18 ++-- addthis.module | 14 --- js/addthis.js | 114 +++++------------------ src/AddThisScriptManager.php | 142 ++--------------------------- src/Element/AddThisBasicButton.php | 21 ++--- 5 files changed, 47 insertions(+), 262 deletions(-) diff --git a/addthis.libraries.yml b/addthis.libraries.yml index fa5b978..f4cc3c5 100644 --- a/addthis.libraries.yml +++ b/addthis.libraries.yml @@ -2,19 +2,15 @@ addthis.admin: version: VERSION css: theme: - http://cache.addthiscdn.com/icons/v1/sprites/services.css: { type: external } + http://cache.addthiscdn.com/icons/v1/sprites/services.css: {} css/addthis.admin.css: {} - -addthis.global: - version: VERSION - js: - js/addthis.js: { scope: footer } - dependencies: - - core/jquery - - core/drupal - - core/drupalSettings addthis.widget: version: VERSION js: - http://s7.addthis.com/js/300/addthis_widget.js: { type: external, scope: footer } \ No newline at end of file + 'http://s7.addthis.com/js/300/addthis_widget.js': {type: external} + js/addthis.js: {} + dependencies: + - core/jquery + - core/drupal + - core/drupalSettings diff --git a/addthis.module b/addthis.module index c8d823c..0f5945f 100644 --- a/addthis.module +++ b/addthis.module @@ -20,20 +20,6 @@ function addthis_theme() { ]; } -/** - * @TODO Find another way to do this since it breaks aggregation. - * @param array $page - */ -function addthis_page_attachments(array &$page) { - $page['#attached']['library'][] = 'addthis/addthis.global'; -} - - -function addthis_library_info_alter() { - -} - - /** * Implementation to retrieve formatters for a given type of field. */ diff --git a/js/addthis.js b/js/addthis.js index a87c593..19d1c6b 100644 --- a/js/addthis.js +++ b/js/addthis.js @@ -1,96 +1,24 @@ -/** - * @file - * AddThis javascript actions. - */ - -(function ($, Drupal, drupalSettings) { - Drupal.behaviors.addthis = { - scriptLoaded: false, - attach: function (context, settings) { - - // The addthis configuration is not loaded but the settings are passed - // to do so. - if (!this.isConfigLoaded() && this.isSettingsLoaded()) { - // Initialize config. - this.initConfig(); - - // Load widget js. - if (!this.scriptLoaded) { - this.loadDomready(); - } - } - // The addthis configuration is not loaded but no setting are available - // to load anything. - else if (!this.isConfigLoaded() && !this.isSettingsLoaded()) { - // Do nothing - } - - // Trigger ready on ajax attach. - if (context != window.document) { - Drupal.behaviors.addthis.ajaxLoad(context, settings); - } - - }, - - // Returns if the settings defined by the addthis module are loaded. - isSettingsLoaded: function () { - if (typeof drupalSettings.addthis == 'undefined') { - return false; - } - return true; - }, - - // Returns if the configuration variables needed for widget.js are defined. - isConfigLoaded: function () { - if (typeof addthis_config == 'undefined' || typeof addthis_share == 'undefined') { - return false; - } - return true; - }, - - initConfig: function () { - addthis_config = drupalSettings.addthis.addthis_config; - addthis_share = drupalSettings.addthis.addthis_share; - }, - - // Load the js library when the dom is ready. - loadDomready: function () { - // If settings asks for loading the script after the dom is loaded, then - // load the script here. - if (!this.scriptLoaded && - this.isConfigLoaded() && - drupalSettings.addthis.domready) { - $.getScript(drupalSettings.addthis.widget_url, Drupal.behaviors.addthis.scriptReady); - } - }, - - // Callback for loading the widget.js dynamically. - scriptReady: function () { - this.scriptLoaded = true; - }, - - // Called when a ajax request returned. - ajaxLoad: function (context, settings) { - if (typeof window.addthis != 'undefined' && - typeof window.addthis.toolbox == 'function') { - window.addthis.toolbox('.addthis_toolbox'); - } +// AddThis requires a global variable. :( +var addthis_config; +var addthis_share; + +(function ($) { + Drupal.behaviors.addThisWidget = { + attach: function (context, settings) { + // Because we cannot dynamically add JS, we do it here. :( + $('body').once('LoadAddThisWidget').each(function () { + //if (typeof settings.addThisWidget.widgetScript !== 'undefined') { + // $.getScript(settings.addThisWidget.widgetScript); + //} + + if (typeof settings.addThisWidget.config !== 'undefined') { + addthis_config = settings.addThisWidget.config; } - }; - - // This load the config in time to run any addthis functionality. - if (Drupal.behaviors.addthis.isConfigLoaded()) { - addthis_config = drupalSettings.addthis.addthis_config; - addthis_share = drupalSettings.addthis.addthis_share; + if (typeof settings.addThisWidget.share !== 'undefined') { + addthis_share = settings.addThisWidget.share; + } + }); } - - // Document ready in case we want to load AddThis into the dom after every - // thing is loaded. - // - // Is executed once after the attach happened. - $(document).ready(function () { - Drupal.behaviors.addthis.loadDomready(); - }); - -}(jQuery, Drupal, drupalSettings)); + }; +})(jQuery); diff --git a/src/AddThisScriptManager.php b/src/AddThisScriptManager.php index deb642d..34df399 100644 --- a/src/AddThisScriptManager.php +++ b/src/AddThisScriptManager.php @@ -22,10 +22,6 @@ class AddThisScriptManager { - - private $async = NULL; - private $domready = NULL; - /** * @var \Drupal\Core\Language\LanguageManager */ @@ -45,125 +41,6 @@ class AddThisScriptManager { public function __construct(\Drupal\Core\Language\LanguageManager $languageManager, \Drupal\Core\Config\ConfigFactory $configFactory) { $this->language_manager = $languageManager; $this->config_factory = $configFactory; - - // TODO: Remove these from here. They can be referenced when we use them. - $config = $this->config_factory->get('addthis.settings.advanced'); - $this->async = $config->get('addthis_widget_load_async'); - $this->domready = $config->get('addthis_widget_load_domready'); - } - - /** - * Get the current widget js url. - * - * @return string - * A url reference to the widget js. - */ - public function getWidgetJsUrl() { - // TODO: This uses a global function, why do we need to check the URL at this point? - return check_url($this->config_factory->get('addthis.settings.advanced')->get('addthis_widget_js_url')); - } - - /** - * Return if we are on https connection. - * - * TODO: Why are we doing all of this URL processing? - * @return bool - * TRUE if the current request is on https. - */ - public function isHttps() { - global $is_https; - - return $is_https; - } - - /** - * Change the schema from http to https if we are on https. - * - * TODO: Why are we doing all of this URL processing? - * @param string $url - * A full url. - * - * @return string - * The changed url. - */ - public function correctSchemaIfHttps($url) { - if (is_string($url) && $this->isHttps()) { - return str_replace('http://', 'https://', $url); - } - else { - return $url; - } - throw new InvalidArgumentException('The argument was not a string value'); - } - - /** - * Attach the widget js to the element. - * - * @todo Change the scope of the addthis.js. - * See if we can get the scope of the addthis.js into the header - * just below the settings so that the settings can be used in the loaded - * addthis.js of our module. - * - * @param array $element - * The element to attach the JavaScript to. - */ - public function attachJsToElement(&$element) { - $config = $this->config_factory->get('addthis.settings'); - $adv_config = $this->config_factory->get('addthis.settings.advanced'); - if ($adv_config->get('addthis_widget_js_include') !== 0) { - $widget_js = new AddThisWidgetJsUrl($adv_config->get('addthis_widget_js_url')); - - - $pubid = $config->get('analytics.addthis_profile_id'); - if (isset($pubid) && !empty($pubid) && is_string($pubid)) { - $widget_js->addAttribute('pubid', $pubid); - } - - $async = $this->async; - if ($async) { - $widget_js->addAttribute('async', 1); - } - - if ($this->domready) { - $widget_js->addAttribute('domready', 1); - } - else { - // Only when the script is not loaded after the DOM is ready we include - // the script with #attached. - $element['#attached']['library'][] = 'addthis/addthis.widget'; - } - - // Every setting value passed here overrides previously set values but - // leaves the values that are already set somewhere else and that are not - // passed here. - $element['#attached']['drupalSettings']['addthis'] = array( - 'async' => $async, - 'domready' => $this->domready, - 'widget_url' => $this->getWidgetJsUrl(), - 'addthis_config' => $this->getJsAddThisConfig(), - 'addthis_share' => $this->getJsAddThisShare(), - ); - } - } - - /** - * Enable / disable domready loading. - * - * @param bool $enabled - * TRUE to enabled domready loading. - */ - function setDomReady($enabled) { - $this->domready = $enabled; - } - - /** - * Enable / disable async loading. - * - * @param bool $enabled - * TRUE to enabled async loading. - */ - function setAsync($enabled) { - $this->async = $enabled; } /** @@ -176,13 +53,13 @@ function setAsync($enabled) { * @todo Make the adding of configuration dynamic. * SRP is lost here. */ - private function getJsAddThisConfig() { + public function getAddThisConfig() { $config = $this->config_factory->get('addthis.settings'); $enabled_services = $this->getServiceNamesAsCommaSeparatedString($config->get('compact_menu.enabled_services.addthis_enabled_services')) . 'more'; $excluded_services = $this->getServiceNamesAsCommaSeparatedString($config->get('excluded_services.addthis_excluded_services')); - $configuration = array( + $configuration = [ 'pubid' => $config->get('analytics.addthis_profile_id'), 'services_compact' => $enabled_services, 'services_exclude' => $excluded_services, @@ -196,9 +73,8 @@ private function getJsAddThisConfig() { 'ui_open_windows' => $config->get('compact_menu.menu_style.addthis_open_windows_enabled'), 'ui_use_css' => $config->get('compact_menu.additionals.addthis_standard_css_enabled'), 'ui_use_addressbook' => $config->get('compact_menu.additionals.addthis_addressbook_enabled'), - // TODO: check that this returns what we want. - 'ui_language' => $this->language_manager->getCurrentLanguage(), - ); + 'ui_language' => $this->language_manager->getCurrentLanguage()->getId(), + ]; // TODO: Do we need to check if the module exists or can we just check the setting? if (\Drupal::moduleHandler()->moduleExists('googleanalytics')) { if ($config->get('analytics.addthis_google_analytics_tracking_enabled')) { @@ -221,14 +97,14 @@ private function getJsAddThisConfig() { * @todo Make the adding of configuration dynamic. * SRP is lost here. */ - private function getJsAddThisShare() { + public function getAddThisShareConfig() { - $configuration = $this->getJsAddThisConfig(); + $configuration = $this->getAddThisConfig(); if (isset($configuration['templates'])) { - $addthis_share = array( + $addthis_share = [ 'templates' => $configuration['templates'], - ); + ]; } $addthis_share['templates']['twitter'] = $this->config_factory->get('addthis.settings') ->get('third_party.addthis_twitter_template'); @@ -243,7 +119,7 @@ private function getJsAddThisShare() { * @param $services * @return string */ - public function getServiceNamesAsCommaSeparatedString($services) { + protected function getServiceNamesAsCommaSeparatedString($services) { $serviceNames = array_values($services); $servicesAsCommaSeparatedString = ''; foreach ($serviceNames as $serviceName) { diff --git a/src/Element/AddThisBasicButton.php b/src/Element/AddThisBasicButton.php index a866813..b6e61f8 100644 --- a/src/Element/AddThisBasicButton.php +++ b/src/Element/AddThisBasicButton.php @@ -45,19 +45,18 @@ public function preRender($element) { ]; // Add Script. - // TODO: Figure out what JS should be attached here. - $config_factory = \Drupal::getContainer()->get('config.factory'); - $config = $config_factory->get('addthis.settings'); - $adv_config = $config_factory->get('addthis.settings.advanced'); - $element['#attached']['library'][] = 'addthis/addthis.widget'; - /** - * Every setting value passed here overrides previously set values but - * leaves the values that are already set somewhere else and that are not - * passed here. - */ - $element['#attached']['drupalSettings']['addthis'] = []; + $script_manager = \Drupal::getContainer()->get('addthis.script_manager'); + + $addThisConfig = $script_manager->getAddThisConfig(); + $addThisShareConfig = $script_manager->getAddThisShareConfig(); + + $element['#attached']['drupalSettings']['addThisWidget'] = [ + 'widgetScript' => 'http://example.dev/thing.js', + 'config' => $addThisConfig, + 'share' => $addThisShareConfig, + ]; return $element; } From d718e0571f6f9ee558a72703b4301a1231c59a25 Mon Sep 17 00:00:00 2001 From: Doyle Date: Tue, 18 Aug 2015 11:56:33 -0400 Subject: [PATCH 36/77] Updated AddThisBasicToolbox to render proper markup. --- .../src/Plugin/Block/AddThisBlock.php | 2 + src/Element/AddThisBasicToolbox.php | 110 ++++++++++++++++++ src/Element/AddThisElement.php | 18 +++ src/Element/AddThisWrapper.php | 20 ++++ templates/addthis-element.html.twig | 6 +- templates/addthis-wrapper.html.twig | 9 +- 6 files changed, 158 insertions(+), 7 deletions(-) diff --git a/addthis_block/src/Plugin/Block/AddThisBlock.php b/addthis_block/src/Plugin/Block/AddThisBlock.php index a7c8228..150883c 100644 --- a/addthis_block/src/Plugin/Block/AddThisBlock.php +++ b/addthis_block/src/Plugin/Block/AddThisBlock.php @@ -185,6 +185,8 @@ public function build() { case 'addthis_basic_toolbox': return [ '#type' => 'addthis_basic_toolbox', + '#size' => $config['basic_toolbox']['buttons_size'], + '#services' => $config['basic_toolbox']['share_services'], ]; break; } diff --git a/src/Element/AddThisBasicToolbox.php b/src/Element/AddThisBasicToolbox.php index 20a05e4..eee757f 100644 --- a/src/Element/AddThisBasicToolbox.php +++ b/src/Element/AddThisBasicToolbox.php @@ -20,9 +20,119 @@ class AddThisBasicToolbox extends RenderElement { * {@inheritdoc} */ public function getInfo() { + $class = get_class($this); return [ '#theme' => 'addthis_basic_toolbox', + '#size' => '', + '#services' => array(), + '#pre_render' => [ + [$class, 'preRender'], + ], ]; } + public function preRender($element) { + $element['addthis_basic_toolbox'] = array( + '#type' => 'addthis_wrapper', + '#tag' => 'div', + '#attributes' => array( + 'class' => array( + 'addthis_toolbox', + 'addthis_default_style', + // ($settings['buttons_size'] == AddThis::CSS_32x32 ? AddThis::CSS_32x32 : NULL), + //$settings['extra_css'], + ), + ), + ); + + // Add Script. + $element['#attached']['library'][] = 'addthis/addthis.widget'; + + $script_manager = \Drupal::getContainer()->get('addthis.script_manager'); + + $addThisConfig = $script_manager->getAddThisConfig(); + $addThisShareConfig = $script_manager->getAddThisShareConfig(); + + $element['#attached']['drupalSettings']['addThisWidget'] = [ + 'widgetScript' => 'http://example.dev/thing.js', + 'config' => $addThisConfig, + 'share' => $addThisShareConfig, + ]; + + $services = trim($element['#services']); + $services = str_replace(' ', '', $services); + $services = explode(',', $services); + // All service elements + $items = array(); + foreach ($services as $service) { + $items[$service] = array( + '#type' => 'addthis_element', + '#tag' => 'a', + 'value' => '', + '#attributes' => array( + //'href' => $script_manager->getBaseBookmarkUrl(), + 'class' => array( + 'addthis_button_' . $service, + ), + ), + '#addthis_service' => $service, + ); + + // Add individual counters. + if (strpos($service, 'counter_') === 0) { + $items[$service]['#attributes']['class'] = array("addthis_$service"); + } + + // Basic implementations of bubble counter orientation. + // @todo Figure all the bubbles out and add them. + // Still missing: tweetme, hyves and stubleupon, google_plusone_badge. + // + $orientation = '';//($settings['counter_orientation'] == 'horizontal' ? TRUE : FALSE); + switch ($service) { + case 'linkedin_counter': + $items[$service]['#attributes'] += array( + 'li:counter' => ($orientation ? '' : 'top'), + ); + break; + case 'facebook_like': + $items[$service]['#attributes'] += array( + 'fb:like:layout' => ($orientation ? 'button_count' : 'box_count') + ); + break; + case 'facebook_share': + $items[$service]['#attributes'] += array( + 'fb:share:layout' => ($orientation ? 'button_count' : 'box_count') + ); + break; + case 'google_plusone': + $items[$service]['#attributes'] += array( + 'g:plusone:size' => ($orientation ? 'standard' : 'tall') + ); + break; + case 'tweet': + $items[$service]['#attributes'] += array( + 'tw:count' => ($orientation ? 'horizontal' : 'vertical'), + 'tw:via' => $script_manager->getTwitterVia(), + ); + break; + case 'bubble_style': + $items[$service]['#attributes']['class'] = array( + 'addthis_counter', + 'addthis_bubble_style' + ); + break; + case 'pill_style': + $items[$service]['#attributes']['class'] = array( + 'addthis_counter', + 'addthis_pill_style' + ); + break; + } + } + + $element['addthis_basic_toolbox'] += $items; + + return $element; + } + } \ No newline at end of file diff --git a/src/Element/AddThisElement.php b/src/Element/AddThisElement.php index 8bda7dc..cab6bec 100644 --- a/src/Element/AddThisElement.php +++ b/src/Element/AddThisElement.php @@ -20,9 +20,27 @@ class AddThisElement extends RenderElement { * {@inheritdoc} */ public function getInfo() { + $class = get_class($this); return [ '#theme' => 'addthis_element', + '#attributes' => array(), + '#pre_render' => [ + [$class, 'preRender'], + ], ]; } + /** + * PreRender function for addthis_element + * + * @TODO: Figure out why twig doesn't handle #attributes OOTB. + * + * @param $element + * @return mixed + */ + public function preRender($element){ + $element['#attributes'] = new Attribute($element['#attributes']); + return $element; + } + } \ No newline at end of file diff --git a/src/Element/AddThisWrapper.php b/src/Element/AddThisWrapper.php index 0963513..c7cc011 100644 --- a/src/Element/AddThisWrapper.php +++ b/src/Element/AddThisWrapper.php @@ -21,9 +21,29 @@ class AddThisWrapper extends RenderElement { * {@inheritdoc} */ public function getInfo() { + $class = get_class($this); return array( '#theme' => 'addthis_wrapper', + '#pre_render' => [ + [$class, 'preRender'], + ], ); } + + public function preRender($element){ + $element['#attributes'] = new Attribute($element['#attributes']); + $children = Element::children($element); + $element['services'] = []; + if (count($children) > 0) { + foreach ($children as $child) { + //@TODO Figure out why we need to call render() here and it isn't handled + //by printing {{element}} in the twig template. + $element['services'][] = render($element[$child]); + } + } + + return $element; + } + } \ No newline at end of file diff --git a/templates/addthis-element.html.twig b/templates/addthis-element.html.twig index 2b25958..5835669 100644 --- a/templates/addthis-element.html.twig +++ b/templates/addthis-element.html.twig @@ -7,8 +7,6 @@ * */ #} -
addthis element
-{{ kint() }} -
+<{{ element['#tag'] }} {{ element['#attributes'] }}> {{ element }} -
+ diff --git a/templates/addthis-wrapper.html.twig b/templates/addthis-wrapper.html.twig index 9d49f0c..a535da6 100644 --- a/templates/addthis-wrapper.html.twig +++ b/templates/addthis-wrapper.html.twig @@ -7,6 +7,9 @@ * */ #} -
- {{ element }} -
+<{{ element['#tag'] }} {{ element['#attributes'] }}> + {% for service in element.services %} + {{ service }} + {% endfor %} + + From 11bde8519af4115e0444b2ba06d409786f878362 Mon Sep 17 00:00:00 2001 From: Doyle Date: Tue, 18 Aug 2015 14:45:00 -0400 Subject: [PATCH 37/77] Updated attributes per findings from Dan. Fixed nesting of elements so they render properly without any additional preRender functionality --- src/Element/AddThisElement.php | 15 --------------- src/Element/AddThisWrapper.php | 20 ++++++++++---------- templates/addthis-element.html.twig | 2 +- templates/addthis-wrapper.html.twig | 6 ++---- 4 files changed, 13 insertions(+), 30 deletions(-) diff --git a/src/Element/AddThisElement.php b/src/Element/AddThisElement.php index cab6bec..e7a4613 100644 --- a/src/Element/AddThisElement.php +++ b/src/Element/AddThisElement.php @@ -24,23 +24,8 @@ public function getInfo() { return [ '#theme' => 'addthis_element', '#attributes' => array(), - '#pre_render' => [ - [$class, 'preRender'], - ], ]; } - /** - * PreRender function for addthis_element - * - * @TODO: Figure out why twig doesn't handle #attributes OOTB. - * - * @param $element - * @return mixed - */ - public function preRender($element){ - $element['#attributes'] = new Attribute($element['#attributes']); - return $element; - } } \ No newline at end of file diff --git a/src/Element/AddThisWrapper.php b/src/Element/AddThisWrapper.php index c7cc011..da13a97 100644 --- a/src/Element/AddThisWrapper.php +++ b/src/Element/AddThisWrapper.php @@ -32,16 +32,16 @@ public function getInfo() { public function preRender($element){ - $element['#attributes'] = new Attribute($element['#attributes']); - $children = Element::children($element); - $element['services'] = []; - if (count($children) > 0) { - foreach ($children as $child) { - //@TODO Figure out why we need to call render() here and it isn't handled - //by printing {{element}} in the twig template. - $element['services'][] = render($element[$child]); - } - } + //$element['#attributes'] = new Attribute($element['#attributes']); +// $children = Element::children($element); +// $element['services'] = []; +// if (count($children) > 0) { +// foreach ($children as $child) { +// //@TODO Figure out why we need to call render() here and it isn't handled +// //by printing {{element}} in the twig template. +// $element['services'][] = render($element[$child]); +// } +// } return $element; } diff --git a/templates/addthis-element.html.twig b/templates/addthis-element.html.twig index 5835669..ec2554a 100644 --- a/templates/addthis-element.html.twig +++ b/templates/addthis-element.html.twig @@ -7,6 +7,6 @@ * */ #} -<{{ element['#tag'] }} {{ element['#attributes'] }}> +<{{ element['#tag'] }} {{ attributes }}> {{ element }} diff --git a/templates/addthis-wrapper.html.twig b/templates/addthis-wrapper.html.twig index a535da6..e1a1519 100644 --- a/templates/addthis-wrapper.html.twig +++ b/templates/addthis-wrapper.html.twig @@ -7,9 +7,7 @@ * */ #} -<{{ element['#tag'] }} {{ element['#attributes'] }}> - {% for service in element.services %} - {{ service }} - {% endfor %} +<{{ element['#tag'] }} {{ attributes }}> + {{ element }} From 3ed848e57a3d590f44d6ec875070b256a1f258d0 Mon Sep 17 00:00:00 2001 From: Doyle Date: Tue, 18 Aug 2015 14:45:26 -0400 Subject: [PATCH 38/77] Removed deprecated code --- src/Element/AddThisWrapper.php | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/Element/AddThisWrapper.php b/src/Element/AddThisWrapper.php index da13a97..80f9f48 100644 --- a/src/Element/AddThisWrapper.php +++ b/src/Element/AddThisWrapper.php @@ -24,26 +24,8 @@ public function getInfo() { $class = get_class($this); return array( '#theme' => 'addthis_wrapper', - '#pre_render' => [ - [$class, 'preRender'], - ], ); } - public function preRender($element){ - //$element['#attributes'] = new Attribute($element['#attributes']); -// $children = Element::children($element); -// $element['services'] = []; -// if (count($children) > 0) { -// foreach ($children as $child) { -// //@TODO Figure out why we need to call render() here and it isn't handled -// //by printing {{element}} in the twig template. -// $element['services'][] = render($element[$child]); -// } -// } - - return $element; - } - } \ No newline at end of file From 24a10dcd9103dd48c80cec4e5aa3eb4ecf97be83 Mon Sep 17 00:00:00 2001 From: Doyle Date: Tue, 18 Aug 2015 15:48:13 -0400 Subject: [PATCH 39/77] Fixed syntax and removed deprecated code --- src/Element/AddThisElement.php | 3 +-- src/Element/AddThisWrapper.php | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Element/AddThisElement.php b/src/Element/AddThisElement.php index e7a4613..b72c9d3 100644 --- a/src/Element/AddThisElement.php +++ b/src/Element/AddThisElement.php @@ -20,10 +20,9 @@ class AddThisElement extends RenderElement { * {@inheritdoc} */ public function getInfo() { - $class = get_class($this); return [ '#theme' => 'addthis_element', - '#attributes' => array(), + '#attributes' => [], ]; } diff --git a/src/Element/AddThisWrapper.php b/src/Element/AddThisWrapper.php index 80f9f48..7ea8ac2 100644 --- a/src/Element/AddThisWrapper.php +++ b/src/Element/AddThisWrapper.php @@ -21,7 +21,6 @@ class AddThisWrapper extends RenderElement { * {@inheritdoc} */ public function getInfo() { - $class = get_class($this); return array( '#theme' => 'addthis_wrapper', ); From 85e093f533a4fefda31d3815009aef42568220f5 Mon Sep 17 00:00:00 2001 From: Doyle Date: Wed, 19 Aug 2015 09:07:38 -0400 Subject: [PATCH 40/77] Removed AddThisWidgetJsURL as it was not being used in the port. We should be using the D8 URL class when re-integrating these peices. --- src/Util/AddThisWidgetJsUrl.php | 53 --------------------------------- 1 file changed, 53 deletions(-) delete mode 100644 src/Util/AddThisWidgetJsUrl.php diff --git a/src/Util/AddThisWidgetJsUrl.php b/src/Util/AddThisWidgetJsUrl.php deleted file mode 100644 index a6dac88..0000000 --- a/src/Util/AddThisWidgetJsUrl.php +++ /dev/null @@ -1,53 +0,0 @@ -url = $url; - } - - /** - * Add a attribute to the querystring. - */ - public function addAttribute($name, $value) { - $this->attributes[$name] = $value; - } - - /** - * Remove a attribute from the querystring. - */ - public function removeAttribute($name) { - if (isset($attributes[$name])) { - unset($attributes[$name]); - } - } - - /** - * Get the full url for the widgetjs. - */ - public function getFullUrl() { - $querystring_elements = array(); - foreach ($this->attributes as $key => $value) { - if (!empty($value)) { - $querystring_elements[] = $key . '=' . $value; - } - } - - $querystring = implode('&', $querystring_elements); - - return $this->url . '#' . $querystring; - } - -} From d8d1ebcae47c056c77ac095e151458aaad63e85a Mon Sep 17 00:00:00 2001 From: Doyle Date: Wed, 19 Aug 2015 09:09:23 -0400 Subject: [PATCH 41/77] Removed reference to deprecated class --- src/AddThisScriptManager.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/AddThisScriptManager.php b/src/AddThisScriptManager.php index 34df399..e2fc10f 100644 --- a/src/AddThisScriptManager.php +++ b/src/AddThisScriptManager.php @@ -18,8 +18,6 @@ namespace Drupal\addthis; -use Drupal\addthis\Util\AddThisWidgetJsUrl; - class AddThisScriptManager { /** From 967dc679722bd41385a8fe9eda3fe273feb67891 Mon Sep 17 00:00:00 2001 From: Doyle Date: Wed, 19 Aug 2015 10:42:36 -0400 Subject: [PATCH 42/77] Added new traits for shared partial config forms for AddThisBasicButton and AddThisBasicToolbox. Updated code where these are used in Block/Field implementations. Removed deprecated code fomr AddThis.php class --- .../src/Plugin/Block/AddThisBlock.php | 83 ++++---- .../AddThisBasicButtonFormatter.php | 6 +- .../AddThisBasicToolboxFormatter.php | 7 +- src/AddThis.php | 189 ------------------ src/AddThisBasicButtonFormTrait.php | 62 ++++++ src/AddThisBasicToolboxFormTrait.php | 91 +++++++++ 6 files changed, 195 insertions(+), 243 deletions(-) create mode 100644 src/AddThisBasicButtonFormTrait.php create mode 100644 src/AddThisBasicToolboxFormTrait.php diff --git a/addthis_block/src/Plugin/Block/AddThisBlock.php b/addthis_block/src/Plugin/Block/AddThisBlock.php index 150883c..50f5e76 100644 --- a/addthis_block/src/Plugin/Block/AddThisBlock.php +++ b/addthis_block/src/Plugin/Block/AddThisBlock.php @@ -6,9 +6,10 @@ namespace Drupal\addthis_block\Plugin\Block; +use Drupal\addthis\AddThisBasicButtonFormTrait; +use Drupal\addthis\AddThisBasicToolboxFormTrait; use Drupal\Core\Block\BlockBase; use Drupal\Core\Form\FormStateInterface; -use Drupal\addthis\AddThis; /** * Provides my custom block. @@ -21,6 +22,9 @@ */ class AddThisBlock extends BlockBase { + use AddThisBasicButtonFormTrait; + use AddThisBasicToolboxFormTrait; + /** * {@inheritdoc} */ @@ -89,14 +93,12 @@ function blockForm($form, FormStateInterface $form_state) { '#suffix' => '', ); if ($type == 'addthis_basic_toolbox') { - $basicToolbox = $add_this_service - ->getBasicToolboxForm($this, $settings['basic_toolbox']); + $basicToolbox = $this->addThisBasicToolboxForm($this, $settings['basic_toolbox']); $form['settings']['addthis_settings']['type_settings']['basic_toolbox'] = $basicToolbox; } else { if ($type == 'addthis_basic_button') { - $basicButton = $add_this_service - ->getBasicButtonForm($this, $settings['basic_button']); + $basicButton = $this->addThisBasicButtonForm($this, $settings['basic_button']); $form['settings']['addthis_settings']['type_settings']['basic_button'] = $basicButton; } } @@ -119,53 +121,40 @@ public function addthisAjaxCallback(array $form, FormStateInterface $form_state) * {@inheritdoc} */ public function blockSubmit($form, FormStateInterface $form_state) { + + //@TODO Settings for unselected type get wiped because they dont exist in form. + // Try not to overwrite them if possible. $this->configuration['type'] = $form_state->getValue([ 'settings', 'addthis_settings', 'type' ]); - $this->configuration['basic_toolbox']['share_services'] = $form_state->getValue([ - 'settings', - 'addthis_settings', - 'type_settings', - 'basic_toolbox', - 'share_services' - ]); - $this->configuration['basic_toolbox']['buttons_size'] = $form_state->getValue([ - 'settings', - 'addthis_settings', - 'type_settings', - 'basic_toolbox', - 'buttons_size' - ]); - $this->configuration['basic_toolbox']['counter_orientation'] = $form_state->getValue([ - 'settings', - 'addthis_settings', - 'type_settings', - 'basic_toolbox', - 'counter_orientation' - ]); - $this->configuration['basic_toolbox']['extra_css'] = $form_state->getValue([ - 'settings', - 'addthis_settings', - 'type_settings', - 'basic_toolbox', - 'extra_css' - ]); - $this->configuration['basic_button']['button_size'] = $form_state->getValue([ - 'settings', - 'addthis_settings', - 'type_settings', - 'basic_button', - 'button_size' - ]); - $this->configuration['basic_button']['extra_css'] = $form_state->getValue([ - 'settings', - 'addthis_settings', - 'type_settings', - 'basic_button', - 'extra_css' - ]); + + //Handle saving the partial elements provided by AddThisBasicToolboxFormTrait. + $basicToolboxKeys = $this->addThisBasicToolboxGetDefaults(); + foreach($basicToolboxKeys as $key => $value) { + $this->configuration['basic_toolbox'][$key] = $form_state->getValue([ + 'settings', + 'addthis_settings', + 'type_settings', + 'basic_toolbox', + $key + ]); + } + + //Handle saving the partial elements provided by AddThisBasicButtonFormTrait. + $basicButtonKeys = $this->addThisBasicButtonGetDefaults(); + foreach($basicButtonKeys as $key => $value) { + $this->configuration['basic_button'][$key] = $form_state->getValue([ + 'settings', + 'addthis_settings', + 'type_settings', + 'basic_button', + $key + ]); + } + + } diff --git a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php index e557b64..e016ae9 100644 --- a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php +++ b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php @@ -6,6 +6,7 @@ namespace Drupal\addthis_fields\Plugin\Field\FieldFormatter; +use Drupal\addthis\AddThisBasicButtonFormTrait; use Drupal\Core\Field\FormatterBase; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; @@ -25,6 +26,7 @@ */ class AddThisBasicButtonFormatter extends FormatterBase { + use AddThisBasicButtonFormTrait; /** * {@inheritdoc} */ @@ -40,9 +42,7 @@ public static function defaultSettings() { */ public function settingsForm(array $form, FormStateInterface $form_state) { $settings = $this->getSettings(); - - $add_this_service = \Drupal::service('addthis.addthis'); - $element = $add_this_service->getBasicButtonForm($this, $settings); + $element = $this->addThisBasicButtonForm($this, $settings); return $element; } diff --git a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php index 1a78fae..c203064 100644 --- a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php +++ b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php @@ -6,6 +6,7 @@ namespace Drupal\addthis_fields\Plugin\Field\FieldFormatter; +use Drupal\addthis\AddThisBasicToolboxFormTrait; use Drupal\Core\Field\FormatterBase; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; @@ -24,6 +25,7 @@ */ class AddThisBasicToolboxFormatter extends FormatterBase { + use AddThisBasicToolboxFormTrait; /** * {@inheritdoc} */ @@ -40,11 +42,8 @@ public static function defaultSettings() { * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state) { - $settings = $this->getSettings(); - $element = array(); - $add_this_service = \Drupal::service('addthis.addthis'); - $add_this_service->getBasicToolboxForm($this, $settings); + $element = $this->addThisBasicToolboxForm($this, $settings); return $element; } diff --git a/src/AddThis.php b/src/AddThis.php index 8b29d5d..6ead9e2 100644 --- a/src/AddThis.php +++ b/src/AddThis.php @@ -254,194 +254,5 @@ public function getDisplayTypes() { return $displays; } - /** - * Provides options for the BasicToolboxForm. This is used in field & block - * configurations. - * @param $options - * @return array - */ - public function getBasicToolboxForm($parent_class, $options) { - $element = array(); - - $element['share_services'] = array( - '#title' => t('Services'), - '#type' => 'textfield', - '#size' => 80, - '#default_value' => $options['share_services'], - '#required' => TRUE, - //Validate function is defined in addthis.module. - '#element_validate' => array( - $parent_class, - 'addThisDisplayElementServicesValidate' - ), - '#description' => - t('Specify the names of the sharing services and seperate them with a , (comma). The names on this list are valid.') . - t('Elements that are available but not ont the services list are (!services).', - array('!services' => 'bubble_style, pill_style, tweet, facebook_send, twitter_follow_native, google_plusone, stumbleupon_badge, counter_* (several supported services), linkedin_counter') - ), - ); - $element['buttons_size'] = array( - '#title' => t('Buttons size'), - '#type' => 'select', - '#default_value' => $options['buttons_size'], - '#options' => array( - 'addthis_16x16_style' => t('Small (16x16)'), - 'addthis_32x32_style' => t('Big (32x32)'), - ), - ); - $element['counter_orientation'] = array( - '#title' => t('Counter orientation'), - '#description' => t('Specify the way service counters are oriented.'), - '#type' => 'select', - '#default_value' => $options['counter_orientation'], - '#options' => array( - 'horizontal' => t('Horizontal'), - 'vertical' => t('Vertical'), - ) - ); - $element['extra_css'] = array( - '#title' => t('Extra CSS declaration'), - '#type' => 'textfield', - '#size' => 40, - '#default_value' => $options['extra_css'], - '#description' => t('Specify extra CSS classes to apply to the toolbox'), - ); - - return $element; - } - - - /** - * - * Returns the basicButtonForm elements to be used in the Field and Block implementation. - * - * @param $parent_class - * @param $options - * @return array - */ - public function getBasicButtonForm($parent_class, $options) { - $element = array(); - - $element['button_size'] = array( - '#title' => t('Image'), - '#type' => 'select', - '#default_value' => $options['button_size'], - '#options' => array( - 'small' => t('Small'), - 'big' => t('Big'), - ), - ); - $element['extra_css'] = array( - '#title' => t('Extra CSS declaration'), - '#type' => 'textfield', - '#size' => 40, - '#default_value' => $options['extra_css'], - '#description' => t('Specify extra CSS classes to apply to the button'), - ); - - return $element; - } - /** - * Returns rendered markup for the BasicToolbox display. This will be called - * from both the Field and Block render functions. - * @param $settings - * @return null - */ - function getBasicToolboxMarkup($settings) { - $element = array( - '#type' => 'addthis_wrapper', - '#tag' => 'div', - '#attributes' => array( - 'class' => array( - 'addthis_toolbox', - 'addthis_default_style', - ($settings['buttons_size'] == AddThis::CSS_32x32 ? AddThis::CSS_32x32 : NULL), - $settings['extra_css'], - ), - ), - ); - - // Add the widget script. - $script_manager = \Drupal::service('addthis.script_manager'); - $script_manager->attachJsToElement($element); - - - $services = trim($settings['share_services']); - $services = str_replace(' ', '', $services); - $services = explode(',', $services); - $items = array(); - - // All service elements - $items = array(); - foreach ($services as $service) { - $items[$service] = array( - '#type' => 'addthis_element', - '#tag' => 'a', - 'value' => '', - '#attributes' => array( - 'href' => $script_manager->getBaseBookmarkUrl(), - 'class' => array( - 'addthis_button_' . $service, - ), - ), - '#addthis_service' => $service, - ); - - // Add individual counters. - if (strpos($service, 'counter_') === 0) { - $items[$service]['#attributes']['class'] = array("addthis_$service"); - } - - // Basic implementations of bubble counter orientation. - // @todo Figure all the bubbles out and add them. - // Still missing: tweetme, hyves and stubleupon, google_plusone_badge. - // - $orientation = ($settings['counter_orientation'] == 'horizontal' ? TRUE : FALSE); - switch ($service) { - case 'linkedin_counter': - $items[$service]['#attributes'] += array( - 'li:counter' => ($orientation ? '' : 'top'), - ); - break; - case 'facebook_like': - $items[$service]['#attributes'] += array( - 'fb:like:layout' => ($orientation ? 'button_count' : 'box_count') - ); - break; - case 'facebook_share': - $items[$service]['#attributes'] += array( - 'fb:share:layout' => ($orientation ? 'button_count' : 'box_count') - ); - break; - case 'google_plusone': - $items[$service]['#attributes'] += array( - 'g:plusone:size' => ($orientation ? 'standard' : 'tall') - ); - break; - case 'tweet': - $items[$service]['#attributes'] += array( - 'tw:count' => ($orientation ? 'horizontal' : 'vertical'), - 'tw:via' => $script_manager->getTwitterVia(), - ); - break; - case 'bubble_style': - $items[$service]['#attributes']['class'] = array( - 'addthis_counter', - 'addthis_bubble_style' - ); - break; - case 'pill_style': - $items[$service]['#attributes']['class'] = array( - 'addthis_counter', - 'addthis_pill_style' - ); - break; - } - } - - $element += $items; - - return render($element); - } } \ No newline at end of file diff --git a/src/AddThisBasicButtonFormTrait.php b/src/AddThisBasicButtonFormTrait.php new file mode 100644 index 0000000..3f06aef --- /dev/null +++ b/src/AddThisBasicButtonFormTrait.php @@ -0,0 +1,62 @@ + 'small', + 'extra_css' => '', + ]; + } + + /** + * Returns partial configuration form for the AddThisBasicButton. + * + * @param $parent_class - The class that is requesting the form. + * @param $options - The base configuration for the class. (Block/Field) + * + * @return array - Partial form to provide configuration. + */ + protected function addThisBasicButtonForm($parent_class, $options){ + $element = []; + + $element['button_size'] = [ + '#title' => t('Image'), + '#type' => 'select', + '#default_value' => $options['button_size'], + '#options' => [ + 'small' => t('Small'), + 'big' => t('Big'), + ], + ]; + $element['extra_css'] = [ + '#title' => t('Extra CSS declaration'), + '#type' => 'textfield', + '#size' => 40, + '#default_value' => $options['extra_css'], + '#description' => t('Specify extra CSS classes to apply to the button'), + ]; + + return $element; + } + + + +} \ No newline at end of file diff --git a/src/AddThisBasicToolboxFormTrait.php b/src/AddThisBasicToolboxFormTrait.php new file mode 100644 index 0000000..eba56a0 --- /dev/null +++ b/src/AddThisBasicToolboxFormTrait.php @@ -0,0 +1,91 @@ + 'facebook,twitter', + 'buttons_size' => 'addthis_16x16_style', + 'counter_orientation' => 'horizontal', + 'extra_css' => '', + ]; + } + + /** + * Returns partial configuration form for the AddThisBasicToolbox. + * + * @param $parent_class - The class that is requesting the form. + * @param $options - The base configuration for the class. (Block/Field) + * + * @return array - Partial form to provide configuration. + */ + protected function addThisBasicToolboxForm($parent_class, $options){ + $element = []; + + $element['share_services'] = [ + '#title' => t('Services'), + '#type' => 'textfield', + '#size' => 80, + '#default_value' => $options['share_services'], + '#required' => TRUE, + //Validate function is defined in addthis.module. + '#element_validate' => [ + $parent_class, + 'addThisDisplayElementServicesValidate' + ], + '#description' => + t('Specify the names of the sharing services and seperate them with a , (comma). The names on this list are valid.') . + t('Elements that are available but not ont the services list are (!services).', + ['!services' => 'bubble_style, pill_style, tweet, facebook_send, twitter_follow_native, google_plusone, stumbleupon_badge, counter_* (several supported services), linkedin_counter'] + ), + ]; + $element['buttons_size'] = [ + '#title' => t('Buttons size'), + '#type' => 'select', + '#default_value' => $options['buttons_size'], + '#options' => [ + 'addthis_16x16_style' => t('Small (16x16)'), + 'addthis_32x32_style' => t('Big (32x32)'), + ], + ]; + $element['counter_orientation'] = [ + '#title' => t('Counter orientation'), + '#description' => t('Specify the way service counters are oriented.'), + '#type' => 'select', + '#default_value' => $options['counter_orientation'], + '#options' => [ + 'horizontal' => t('Horizontal'), + 'vertical' => t('Vertical'), + ] + ]; + $element['extra_css'] = [ + '#title' => t('Extra CSS declaration'), + '#type' => 'textfield', + '#size' => 40, + '#default_value' => $options['extra_css'], + '#description' => t('Specify extra CSS classes to apply to the toolbox'), + ]; + + return $element; + } + + + +} \ No newline at end of file From 5d5416bf0ae12a7aa7e51977f4a7d1408dab38fc Mon Sep 17 00:00:00 2001 From: Doyle Date: Wed, 19 Aug 2015 11:15:24 -0400 Subject: [PATCH 43/77] Updated logic for defaults. Moved validation function for services (but still doesn't get fired). --- addthis.module | 22 --------------- .../src/Plugin/Block/AddThisBlock.php | 18 +++++-------- .../AddThisBasicButtonFormatter.php | 2 +- .../AddThisBasicToolboxFormatter.php | 2 +- src/AddThisBasicButtonFormTrait.php | 2 +- src/AddThisBasicToolboxFormTrait.php | 27 +++++++++++++++++-- 6 files changed, 34 insertions(+), 39 deletions(-) diff --git a/addthis.module b/addthis.module index 0f5945f..2125af9 100644 --- a/addthis.module +++ b/addthis.module @@ -35,25 +35,3 @@ function _addthis_field_info_formatter_field_type($field_type = NULL) { } -/** - * Validation for services for BasicToolbox. - * @param array $element - * @param FormStateInterface $form_state - */ -function addThisDisplayElementServicesValidate(array $element, FormStateInterface $form_state) { - $bad = FALSE; - - $services = trim($element['#value']); - $services = str_replace(' ', '', $services); - - if (!preg_match('/^[a-z\_\,0-9]+$/', $services)) { - $bad = TRUE; - } - // @todo Validate the service names against AddThis.com. Give a notice when there are bad names. - - // Return error. - if ($bad) { - form_error($element, t('The declared services are incorrect or nonexistent.')); - } -} - diff --git a/addthis_block/src/Plugin/Block/AddThisBlock.php b/addthis_block/src/Plugin/Block/AddThisBlock.php index 50f5e76..f1885a0 100644 --- a/addthis_block/src/Plugin/Block/AddThisBlock.php +++ b/addthis_block/src/Plugin/Block/AddThisBlock.php @@ -31,16 +31,8 @@ class AddThisBlock extends BlockBase { public function defaultConfiguration() { return array( 'type' => 'addthis_disabled', - 'basic_toolbox' => array( - 'share_services' => 'facebook,twitter', - 'buttons_size' => 'addthis_16x16_style', - 'counter_orientation' => 'horizontal', - 'extra_css' => '', - ), - 'basic_button' => array( - 'buttons_size' => 'addthis_16x16_style', - 'extra_css' => '', - ), + 'basic_toolbox' => $this->addThisBasicToolboxGetDefaults(), + 'basic_button' => $this->addThisBasicButtonGetDefaults(), ); } @@ -52,6 +44,8 @@ function blockForm($form, FormStateInterface $form_state) { // The list of formatters. $add_this_service = \Drupal::service('addthis.addthis'); $formatter_options = $add_this_service->getDisplayTypes(); + + $settings = $this->getConfiguration(); $type = $settings['type']; @@ -93,12 +87,12 @@ function blockForm($form, FormStateInterface $form_state) { '#suffix' => '', ); if ($type == 'addthis_basic_toolbox') { - $basicToolbox = $this->addThisBasicToolboxForm($this, $settings['basic_toolbox']); + $basicToolbox = $this->addThisBasicToolboxForm($settings['basic_toolbox']); $form['settings']['addthis_settings']['type_settings']['basic_toolbox'] = $basicToolbox; } else { if ($type == 'addthis_basic_button') { - $basicButton = $this->addThisBasicButtonForm($this, $settings['basic_button']); + $basicButton = $this->addThisBasicButtonForm($settings['basic_button']); $form['settings']['addthis_settings']['type_settings']['basic_button'] = $basicButton; } } diff --git a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php index e016ae9..aaacea2 100644 --- a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php +++ b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php @@ -42,7 +42,7 @@ public static function defaultSettings() { */ public function settingsForm(array $form, FormStateInterface $form_state) { $settings = $this->getSettings(); - $element = $this->addThisBasicButtonForm($this, $settings); + $element = $this->addThisBasicButtonForm($settings); return $element; } diff --git a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php index c203064..044ea7c 100644 --- a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php +++ b/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php @@ -43,7 +43,7 @@ public static function defaultSettings() { */ public function settingsForm(array $form, FormStateInterface $form_state) { $settings = $this->getSettings(); - $element = $this->addThisBasicToolboxForm($this, $settings); + $element = $this->addThisBasicToolboxForm($settings); return $element; } diff --git a/src/AddThisBasicButtonFormTrait.php b/src/AddThisBasicButtonFormTrait.php index 3f06aef..67987db 100644 --- a/src/AddThisBasicButtonFormTrait.php +++ b/src/AddThisBasicButtonFormTrait.php @@ -34,7 +34,7 @@ protected function addThisBasicButtonGetDefaults(){ * * @return array - Partial form to provide configuration. */ - protected function addThisBasicButtonForm($parent_class, $options){ + protected function addThisBasicButtonForm($options){ $element = []; $element['button_size'] = [ diff --git a/src/AddThisBasicToolboxFormTrait.php b/src/AddThisBasicToolboxFormTrait.php index eba56a0..e8ea5e8 100644 --- a/src/AddThisBasicToolboxFormTrait.php +++ b/src/AddThisBasicToolboxFormTrait.php @@ -36,7 +36,7 @@ protected function addThisBasicToolboxGetDefaults(){ * * @return array - Partial form to provide configuration. */ - protected function addThisBasicToolboxForm($parent_class, $options){ + protected function addThisBasicToolboxForm($options){ $element = []; $element['share_services'] = [ @@ -47,7 +47,7 @@ protected function addThisBasicToolboxForm($parent_class, $options){ '#required' => TRUE, //Validate function is defined in addthis.module. '#element_validate' => [ - $parent_class, + $this, 'addThisDisplayElementServicesValidate' ], '#description' => @@ -87,5 +87,28 @@ protected function addThisBasicToolboxForm($parent_class, $options){ } + /** + * @TODO Find out why this is never being called. + * + * Validation for services for BasicToolbox. + * @param array $element + * @param FormStateInterface $form_state + */ + public static function addThisDisplayElementServicesValidate($element, FormStateInterface $form_state) { + $bad = FALSE; + + $services = trim($element['#value']); + $services = str_replace(' ', '', $services); + + if (!preg_match('/^[a-z\_\,0-9]+$/', $services)) { + $bad = TRUE; + } + // @todo Validate the service names against AddThis.com. Give a notice when there are bad names. + + // Return error. + if ($bad) { + form_error($element, t('The declared services are incorrect or nonexistent.')); + } + } } \ No newline at end of file From 643f60e60e1a0706a2a3674fcc01e4afc93a7f61 Mon Sep 17 00:00:00 2001 From: Doyle Date: Wed, 19 Aug 2015 17:03:10 -0400 Subject: [PATCH 44/77] Resolved syntax to correctly call validation function. Needed to do class::Method instead of a static validation function --- src/AddThisBasicToolboxFormTrait.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/AddThisBasicToolboxFormTrait.php b/src/AddThisBasicToolboxFormTrait.php index e8ea5e8..ae2645c 100644 --- a/src/AddThisBasicToolboxFormTrait.php +++ b/src/AddThisBasicToolboxFormTrait.php @@ -8,6 +8,7 @@ namespace Drupal\addthis; + /** * Class AddThisBasicToolboxFormTrait * @package Drupal\addthis @@ -47,8 +48,7 @@ protected function addThisBasicToolboxForm($options){ '#required' => TRUE, //Validate function is defined in addthis.module. '#element_validate' => [ - $this, - 'addThisDisplayElementServicesValidate' + get_class() . '::addThisDisplayElementServicesValidate' ], '#description' => t('Specify the names of the sharing services and seperate them with a , (comma). The names on this list are valid.') . @@ -89,7 +89,7 @@ protected function addThisBasicToolboxForm($options){ /** * @TODO Find out why this is never being called. - * + * * Validation for services for BasicToolbox. * @param array $element * @param FormStateInterface $form_state @@ -107,7 +107,7 @@ public static function addThisDisplayElementServicesValidate($element, FormState // Return error. if ($bad) { - form_error($element, t('The declared services are incorrect or nonexistent.')); + $form_state->setErrorByName($element['name'], t('The declared services are incorrect or nonexistent.')); } } From 59810c59f8271fcc3244594e0e60c82df4b814a5 Mon Sep 17 00:00:00 2001 From: John Doyle Date: Thu, 20 Aug 2015 09:45:30 -0400 Subject: [PATCH 45/77] Removed logic to have field_formatters control block display. Hard code options of addthis_basic_toolbox and addthis_basic_button for the time being. --- addthis.module | 14 -------------- addthis_block/src/Plugin/Block/AddThisBlock.php | 11 ++++------- src/AddThis.php | 8 -------- 3 files changed, 4 insertions(+), 29 deletions(-) diff --git a/addthis.module b/addthis.module index 2125af9..f1895a0 100644 --- a/addthis.module +++ b/addthis.module @@ -20,18 +20,4 @@ function addthis_theme() { ]; } -/** - * Implementation to retrieve formatters for a given type of field. - */ -function _addthis_field_info_formatter_field_type($field_type = NULL) { - $formatters = \Drupal::Service('plugin.manager.field.formatter') - ->getDefinitions(); - foreach ($formatters as $key => $formatter) { - if (!in_array((!isset($field_type) ? 'addthis' : $field_type), $formatter['field_types'])) { - unset($formatters[$key]); - } - } - return $formatters; -} - diff --git a/addthis_block/src/Plugin/Block/AddThisBlock.php b/addthis_block/src/Plugin/Block/AddThisBlock.php index f1885a0..3ecb228 100644 --- a/addthis_block/src/Plugin/Block/AddThisBlock.php +++ b/addthis_block/src/Plugin/Block/AddThisBlock.php @@ -40,12 +40,6 @@ public function defaultConfiguration() { * {@inheritdoc} */ function blockForm($form, FormStateInterface $form_state) { - - // The list of formatters. - $add_this_service = \Drupal::service('addthis.addthis'); - $formatter_options = $add_this_service->getDisplayTypes(); - - $settings = $this->getConfiguration(); $type = $settings['type']; @@ -74,7 +68,10 @@ function blockForm($form, FormStateInterface $form_state) { '#type' => 'select', '#title' => t('Formatter for @title', array('@title' => 'AddThis block')), '#title_display' => 'invisible', - '#options' => $formatter_options, + '#options' => [ + 'addthis_basic_button', + 'addthis_basic_toolbox' + ], '#default_value' => $settings['type'], '#attributes' => array('class' => array('addthis-display-type')), '#ajax' => array( diff --git a/src/AddThis.php b/src/AddThis.php index 6ead9e2..08dbe07 100644 --- a/src/AddThis.php +++ b/src/AddThis.php @@ -246,13 +246,5 @@ private function getProfileIdQueryParameterPrefixedWithHash() { } - public function getDisplayTypes() { - $displays = array(); - foreach ($display_impl = _addthis_field_info_formatter_field_type() as $key => $display) { - $displays[$key] = t(SafeMarkup::checkPlain($display['label'])); - } - return $displays; - } - } \ No newline at end of file From 4bf2d2a4d073e972886c3b993224c54f8b70538a Mon Sep 17 00:00:00 2001 From: Jaleel Carter Date: Fri, 21 Aug 2015 08:29:34 -0400 Subject: [PATCH 46/77] updated Render Elements to setup the variables. deleted wrapper and element templates --- src/Element/AddThisBasicButton.php | 3 ++- src/Element/AddThisBasicToolbox.php | 5 +++-- templates/addthis-element.html.twig | 12 ------------ templates/addthis-wrapper.html.twig | 13 ------------- 4 files changed, 5 insertions(+), 28 deletions(-) delete mode 100644 templates/addthis-element.html.twig delete mode 100644 templates/addthis-wrapper.html.twig diff --git a/src/Element/AddThisBasicButton.php b/src/Element/AddThisBasicButton.php index b6e61f8..00ffbb5 100644 --- a/src/Element/AddThisBasicButton.php +++ b/src/Element/AddThisBasicButton.php @@ -21,9 +21,10 @@ class AddThisBasicButton extends RenderElement { */ public function getInfo() { $class = get_class($this); + $config = $this->configuration; return [ '#theme' => 'addthis_basic_button', - '#size' => 'small', + '#size' => $config['basic_button']['button_size'], '#pre_render' => [ [$class, 'preRender'], ], diff --git a/src/Element/AddThisBasicToolbox.php b/src/Element/AddThisBasicToolbox.php index eee757f..05f2ba2 100644 --- a/src/Element/AddThisBasicToolbox.php +++ b/src/Element/AddThisBasicToolbox.php @@ -21,10 +21,11 @@ class AddThisBasicToolbox extends RenderElement { */ public function getInfo() { $class = get_class($this); + $config = $this->configuration; return [ '#theme' => 'addthis_basic_toolbox', - '#size' => '', - '#services' => array(), + '#size' => $config['basic_toolbox']['buttons_size'], + '#services' => $config['basic_toolbox']['share_services'], '#pre_render' => [ [$class, 'preRender'], ], diff --git a/templates/addthis-element.html.twig b/templates/addthis-element.html.twig deleted file mode 100644 index ec2554a..0000000 --- a/templates/addthis-element.html.twig +++ /dev/null @@ -1,12 +0,0 @@ -{# -/** - * @file - * Default theme implementation to display a node.. - * Available variables: - * - element: AddThis Wrapper to be rendered - * - */ -#} -<{{ element['#tag'] }} {{ attributes }}> - {{ element }} - diff --git a/templates/addthis-wrapper.html.twig b/templates/addthis-wrapper.html.twig deleted file mode 100644 index e1a1519..0000000 --- a/templates/addthis-wrapper.html.twig +++ /dev/null @@ -1,13 +0,0 @@ -{# -/** - * @file - * Default theme implementation to display a node. - * Available variables: - * - element: original AddThis wrapper object to reference - * - */ -#} -<{{ element['#tag'] }} {{ attributes }}> - {{ element }} - - From b357b07a1d4cdad1e9d6e9d91d1e42b730a3bd32 Mon Sep 17 00:00:00 2001 From: Jaleel Carter Date: Fri, 21 Aug 2015 13:54:12 -0400 Subject: [PATCH 47/77] Clean up AddThisElement & AddThisWrapper pre-render/twig templates --- addthis_block/addthis_block.info.yml | 11 -- addthis_fields/addthis_fields.info.yml | 11 -- src/Element/AddThisBasicButton.php | 1 + src/Element/AddThisBasicToolbox.php | 161 +++++++++--------- src/Element/AddThisElement.php | 30 ---- src/Element/AddThisWrapper.php | 30 ---- .../src => src}/Plugin/Block/AddThisBlock.php | 10 +- .../AddThisBasicButtonFormatter.php | 12 +- .../AddThisBasicToolboxFormatter.php | 13 +- templates/addthis-basic-button.html.twig | 5 +- templates/addthis-basic-toolbox.html.twig | 18 +- 11 files changed, 121 insertions(+), 181 deletions(-) delete mode 100644 addthis_block/addthis_block.info.yml delete mode 100644 addthis_fields/addthis_fields.info.yml delete mode 100644 src/Element/AddThisElement.php delete mode 100644 src/Element/AddThisWrapper.php rename {addthis_block/src => src}/Plugin/Block/AddThisBlock.php (92%) rename {addthis_fields/src => src}/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php (76%) rename {addthis_fields/src => src}/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php (76%) diff --git a/addthis_block/addthis_block.info.yml b/addthis_block/addthis_block.info.yml deleted file mode 100644 index 6c2d411..0000000 --- a/addthis_block/addthis_block.info.yml +++ /dev/null @@ -1,11 +0,0 @@ -name: AddThis Block -description: Provides a configurable block to render AddThis elements. -type: module -package: Sharing -core: 8.x - -dependencies: - - addthis - - - diff --git a/addthis_fields/addthis_fields.info.yml b/addthis_fields/addthis_fields.info.yml deleted file mode 100644 index f50f934..0000000 --- a/addthis_fields/addthis_fields.info.yml +++ /dev/null @@ -1,11 +0,0 @@ -name: AddThis Fields -description: Provides a basic field definitions for rendering AddThis. -type: module -package: Sharing -core: 8.x - -dependencies: - - addthis - - - diff --git a/src/Element/AddThisBasicButton.php b/src/Element/AddThisBasicButton.php index 00ffbb5..2f77aa3 100644 --- a/src/Element/AddThisBasicButton.php +++ b/src/Element/AddThisBasicButton.php @@ -25,6 +25,7 @@ public function getInfo() { return [ '#theme' => 'addthis_basic_button', '#size' => $config['basic_button']['button_size'], + '#extra_classes' => $config['basic_button']['extra_css'], '#pre_render' => [ [$class, 'preRender'], ], diff --git a/src/Element/AddThisBasicToolbox.php b/src/Element/AddThisBasicToolbox.php index 05f2ba2..206e170 100644 --- a/src/Element/AddThisBasicToolbox.php +++ b/src/Element/AddThisBasicToolbox.php @@ -26,6 +26,7 @@ public function getInfo() { '#theme' => 'addthis_basic_toolbox', '#size' => $config['basic_toolbox']['buttons_size'], '#services' => $config['basic_toolbox']['share_services'], + '#extra_classes' => $config['basic_toolbox']['extra_css'], '#pre_render' => [ [$class, 'preRender'], ], @@ -33,18 +34,18 @@ public function getInfo() { } public function preRender($element) { - $element['addthis_basic_toolbox'] = array( - '#type' => 'addthis_wrapper', - '#tag' => 'div', - '#attributes' => array( - 'class' => array( - 'addthis_toolbox', - 'addthis_default_style', - // ($settings['buttons_size'] == AddThis::CSS_32x32 ? AddThis::CSS_32x32 : NULL), - //$settings['extra_css'], - ), - ), - ); +//// $element['addthis_basic_toolbox'] = array( +//// '#type' => 'addthis_wrapper', +//// '#tag' => 'div', +//// '#attributes' => array( +//// 'class' => array( +//// 'addthis_toolbox', +//// 'addthis_default_style', +//// // ($settings['buttons_size'] == AddThis::CSS_32x32 ? AddThis::CSS_32x32 : NULL), +//// //$settings['extra_css'], +//// ), +// ), +// ); // Add Script. $element['#attached']['library'][] = 'addthis/addthis.widget'; @@ -63,75 +64,75 @@ public function preRender($element) { $services = trim($element['#services']); $services = str_replace(' ', '', $services); $services = explode(',', $services); - // All service elements - $items = array(); - foreach ($services as $service) { - $items[$service] = array( - '#type' => 'addthis_element', - '#tag' => 'a', - 'value' => '', - '#attributes' => array( - //'href' => $script_manager->getBaseBookmarkUrl(), - 'class' => array( - 'addthis_button_' . $service, - ), - ), - '#addthis_service' => $service, - ); + $element['services'] = $services; +// // All service elements +// $items = array(); +//// foreach ($services as $service) { +//// $items[$service] = array( +//// '#type' => 'addthis_element', +//// '#tag' => 'a', +//// 'value' => '', +//// '#attributes' => array( +//// //'href' => $script_manager->getBaseBookmarkUrl(), +//// 'class' => array( +//// 'addthis_button_' . $service, +//// ), +//// ), +//// '#addthis_service' => $service, +//// ); +//// +//// // Add individual counters. +//// if (strpos($service, 'counter_') === 0) { +//// $items[$service]['#attributes']['class'] = array("addthis_$service"); +//// } +// +// // Basic implementations of bubble counter orientation. +// // @todo Figure all the bubbles out and add them. +// // Still missing: tweetme, hyves and stubleupon, google_plusone_badge. +// // +// $orientation = '';//($settings['counter_orientation'] == 'horizontal' ? TRUE : FALSE); +// switch ($service) { +// case 'linkedin_counter': +// $items[$service]['#attributes'] += array( +// 'li:counter' => ($orientation ? '' : 'top'), +// ); +// break; +// case 'facebook_like': +// $items[$service]['#attributes'] += array( +// 'fb:like:layout' => ($orientation ? 'button_count' : 'box_count') +// ); +// break; +// case 'facebook_share': +// $items[$service]['#attributes'] += array( +// 'fb:share:layout' => ($orientation ? 'button_count' : 'box_count') +// ); +// break; +// case 'google_plusone': +// $items[$service]['#attributes'] += array( +// 'g:plusone:size' => ($orientation ? 'standard' : 'tall') +// ); +// break; +// case 'tweet': +// $items[$service]['#attributes'] += array( +// 'tw:count' => ($orientation ? 'horizontal' : 'vertical'), +// 'tw:via' => $script_manager->getTwitterVia(), +// ); +// break; +// case 'bubble_style': +// $items[$service]['#attributes']['class'] = array( +// 'addthis_counter', +// 'addthis_bubble_style' +// ); +// break; +// case 'pill_style': +// $items[$service]['#attributes']['class'] = array( +// 'addthis_counter', +// 'addthis_pill_style' +// ); +// break; +// } +// } - // Add individual counters. - if (strpos($service, 'counter_') === 0) { - $items[$service]['#attributes']['class'] = array("addthis_$service"); - } - - // Basic implementations of bubble counter orientation. - // @todo Figure all the bubbles out and add them. - // Still missing: tweetme, hyves and stubleupon, google_plusone_badge. - // - $orientation = '';//($settings['counter_orientation'] == 'horizontal' ? TRUE : FALSE); - switch ($service) { - case 'linkedin_counter': - $items[$service]['#attributes'] += array( - 'li:counter' => ($orientation ? '' : 'top'), - ); - break; - case 'facebook_like': - $items[$service]['#attributes'] += array( - 'fb:like:layout' => ($orientation ? 'button_count' : 'box_count') - ); - break; - case 'facebook_share': - $items[$service]['#attributes'] += array( - 'fb:share:layout' => ($orientation ? 'button_count' : 'box_count') - ); - break; - case 'google_plusone': - $items[$service]['#attributes'] += array( - 'g:plusone:size' => ($orientation ? 'standard' : 'tall') - ); - break; - case 'tweet': - $items[$service]['#attributes'] += array( - 'tw:count' => ($orientation ? 'horizontal' : 'vertical'), - 'tw:via' => $script_manager->getTwitterVia(), - ); - break; - case 'bubble_style': - $items[$service]['#attributes']['class'] = array( - 'addthis_counter', - 'addthis_bubble_style' - ); - break; - case 'pill_style': - $items[$service]['#attributes']['class'] = array( - 'addthis_counter', - 'addthis_pill_style' - ); - break; - } - } - - $element['addthis_basic_toolbox'] += $items; return $element; } diff --git a/src/Element/AddThisElement.php b/src/Element/AddThisElement.php deleted file mode 100644 index b72c9d3..0000000 --- a/src/Element/AddThisElement.php +++ /dev/null @@ -1,30 +0,0 @@ - 'addthis_element', - '#attributes' => [], - ]; - } - - -} \ No newline at end of file diff --git a/src/Element/AddThisWrapper.php b/src/Element/AddThisWrapper.php deleted file mode 100644 index 7ea8ac2..0000000 --- a/src/Element/AddThisWrapper.php +++ /dev/null @@ -1,30 +0,0 @@ - 'addthis_wrapper', - ); - } - - -} \ No newline at end of file diff --git a/addthis_block/src/Plugin/Block/AddThisBlock.php b/src/Plugin/Block/AddThisBlock.php similarity index 92% rename from addthis_block/src/Plugin/Block/AddThisBlock.php rename to src/Plugin/Block/AddThisBlock.php index 3ecb228..066d14f 100644 --- a/addthis_block/src/Plugin/Block/AddThisBlock.php +++ b/src/Plugin/Block/AddThisBlock.php @@ -1,10 +1,10 @@ t('Formatter for @title', array('@title' => 'AddThis block')), '#title_display' => 'invisible', '#options' => [ - 'addthis_basic_button', - 'addthis_basic_toolbox' + 'addthis_basic_button' => 'AddThis Basic Button', + 'addthis_basic_toolbox' => 'AddThis Basic Toolbox', ], '#default_value' => $settings['type'], '#attributes' => array('class' => array('addthis-display-type')), @@ -160,6 +160,7 @@ public function build() { return [ '#type' => 'addthis_basic_button', '#size' => $config['basic_button']['button_size'], + '#extra_classes' => $config['basic_button']['extra_css'], ]; break; case 'addthis_basic_toolbox': @@ -167,6 +168,7 @@ public function build() { '#type' => 'addthis_basic_toolbox', '#size' => $config['basic_toolbox']['buttons_size'], '#services' => $config['basic_toolbox']['share_services'], + '#extra_classes' => $config['basic_toolbox']['extra_css'], ]; break; } diff --git a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php similarity index 76% rename from addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php rename to src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php index aaacea2..6447858 100644 --- a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php @@ -1,10 +1,10 @@ getSettings(); - $add_this_service = \Drupal::service('addthis.addthis'); - $markup = $add_this_service->getBasicButtonMarkup($settings); + return array( - '#markup' => $markup + '#type' => 'addthis_basic_button', + '#size' => $config['basic_button']['button_size'], + '#extra_classes' => $config['basic_button']['extra_css'], ); } diff --git a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php similarity index 76% rename from addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php rename to src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php index 044ea7c..2b8fb56 100644 --- a/addthis_fields/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php @@ -1,10 +1,10 @@ getSettings(); - - $add_this_service = \Drupal::service('addthis.addthis'); - $markup = $add_this_service->getBasicToolboxMarkup($settings); return array( - '#markup' => $markup + '#type' => 'addthis_basic_toolbox', + '#size' => $config['basic_toolbox']['buttons_size'], + '#services' => $config['basic_toolbox']['share_services'], + '#extra_classes' => $config['basic_toolbox']['extra_css'], ); } diff --git a/templates/addthis-basic-button.html.twig b/templates/addthis-basic-button.html.twig index 40e17d0..bb3071e 100644 --- a/templates/addthis-basic-button.html.twig +++ b/templates/addthis-basic-button.html.twig @@ -3,10 +3,13 @@ * @file * Default theme implementation to display a node.. * Available variables: - * - element: AddThis Wrapper to be rendered + * - element: AddThis basic button to be rendered * */ #} + + + {{ element }} diff --git a/templates/addthis-basic-toolbox.html.twig b/templates/addthis-basic-toolbox.html.twig index 791390a..23ce0d9 100644 --- a/templates/addthis-basic-toolbox.html.twig +++ b/templates/addthis-basic-toolbox.html.twig @@ -7,4 +7,20 @@ * */ #} -{{ element }} + +
+{#{{ element }}#} + {% for service in element.services %} + + {% endfor %} +
+ + +{## print all variables#} +{#{{ dump(_context) }}#} +{## print only keys#} +{#{{ dump(_context|keys) }}#} +{## foreach loop for myarray#} +{#{% for service in element.services %}#} +{##} +{#{% endfor %}#} \ No newline at end of file From 9864f8729ed28dbbedccb75737d4b5d712fb22d2 Mon Sep 17 00:00:00 2001 From: Jason Enter Date: Fri, 21 Aug 2015 14:24:18 -0400 Subject: [PATCH 48/77] saving dynamic path updates --- addthis.libraries.yml | 2 +- js/addthis.js | 50 +++++++++++++++++--------- src/AddThisScriptManager.php | 56 +++++++++++++++++++++++++++++ src/Element/AddThisBasicButton.php | 12 +------ src/Element/AddThisBasicToolbox.php | 12 +------ 5 files changed, 93 insertions(+), 39 deletions(-) diff --git a/addthis.libraries.yml b/addthis.libraries.yml index f4cc3c5..b6cba99 100644 --- a/addthis.libraries.yml +++ b/addthis.libraries.yml @@ -8,7 +8,7 @@ addthis.admin: addthis.widget: version: VERSION js: - 'http://s7.addthis.com/js/300/addthis_widget.js': {type: external} + # 'http://s7.addthis.com/js/300/addthis_widget.js': {type: external} js/addthis.js: {} dependencies: - core/jquery diff --git a/js/addthis.js b/js/addthis.js index 19d1c6b..cff8dff 100644 --- a/js/addthis.js +++ b/js/addthis.js @@ -1,24 +1,42 @@ // AddThis requires a global variable. :( -var addthis_config; -var addthis_share; +var addthis_config, + addthis_share; -(function ($) { +(function ($, Drupal, window, document, undefined) { Drupal.behaviors.addThisWidget = { attach: function (context, settings) { - // Because we cannot dynamically add JS, we do it here. :( - $('body').once('LoadAddThisWidget').each(function () { - //if (typeof settings.addThisWidget.widgetScript !== 'undefined') { - // $.getScript(settings.addThisWidget.widgetScript); - //} - - if (typeof settings.addThisWidget.config !== 'undefined') { - addthis_config = settings.addThisWidget.config; - } + if (context === document) { // only fires on document load + + console.log(settings.addThisWidget); + + console.log('loaded '); + + // Because we cannot dynamically add JS, we do it here. :( + if (typeof settings.addThisWidget.widgetScript !== 'undefined') { + $.getScript(settings.addThisWidget.widgetScript) + .done(function (script, textStatus) { + + // console.log(settings.addThisWidget.widgetScript); + + console.log('Success'); - if (typeof settings.addThisWidget.share !== 'undefined') { - addthis_share = settings.addThisWidget.share; + }).fail(function (jqxhr, settings, exception) { + + // TODO: check for fail msg + console.log('Fail'); + + }); } - }); + + /* + console.log(addthis_share); + console.log('- - - - - - - - - - - - '); + console.log(settings.addThisWidget); + console.log('- - - - - - - - - - - - '); + */ + + } } }; -})(jQuery); + +})(jQuery, Drupal, this, this.document); diff --git a/src/AddThisScriptManager.php b/src/AddThisScriptManager.php index e2fc10f..861e684 100644 --- a/src/AddThisScriptManager.php +++ b/src/AddThisScriptManager.php @@ -128,4 +128,60 @@ protected function getServiceNamesAsCommaSeparatedString($services) { return $servicesAsCommaSeparatedString; } + + /** + * Attach the widget js to the element. + * + * @todo Change the scope of the addthis.js. + * See if we can get the scope of the addthis.js into the header + * just below the settings so that the settings can be used in the loaded + * addthis.js of our module. + * + * @param array $element + * The element to attach the JavaScript to. + */ + public function attachJsToElement(&$element) { + + $addthis_url_configs = array(); + $config = $this->config_factory->get('addthis.settings'); + $adv_config = $this->config_factory->get('addthis.settings.advanced'); + + if ($adv_config->get('addthis_widget_js_include') !== 0) { + + $widget_js_url = $adv_config->get('addthis_widget_js_url'); + $pubid = $config->get('analytics.addthis_profile_id'); + + if (isset($pubid) && !empty($pubid) && is_string($pubid)) { + //$widget_js->addAttribute('pubid', $pubid); + } + $async = $this->async; + if ($async) { + //$widget_js->addAttribute('async', 1); + } + if ($this->domready) { + //$widget_js->addAttribute('domready', 1); + } + } + + if(!isset($widget_js_url)) { + $widget_js_url = 'http://s7.addthis.com/js/250/addthis_widget.js'; + } + + $element['#attached']['library'][] = 'addthis/addthis.widget'; + $addThisConfig = $this->getAddThisConfig(); + $addThisShareConfig = $this->getAddThisShareConfig(); + + $element['#attached']['drupalSettings']['addThisWidget'] = [ + 'widgetScript' => $widget_js_url, + 'config' => $addThisConfig, + 'share' => $addThisShareConfig, + ]; + + + } + + } + + + diff --git a/src/Element/AddThisBasicButton.php b/src/Element/AddThisBasicButton.php index b6e61f8..ac7b8a8 100644 --- a/src/Element/AddThisBasicButton.php +++ b/src/Element/AddThisBasicButton.php @@ -44,19 +44,9 @@ public function preRender($element) { '#alt' => t('Share page with AddThis'), ]; - // Add Script. - $element['#attached']['library'][] = 'addthis/addthis.widget'; $script_manager = \Drupal::getContainer()->get('addthis.script_manager'); - - $addThisConfig = $script_manager->getAddThisConfig(); - $addThisShareConfig = $script_manager->getAddThisShareConfig(); - - $element['#attached']['drupalSettings']['addThisWidget'] = [ - 'widgetScript' => 'http://example.dev/thing.js', - 'config' => $addThisConfig, - 'share' => $addThisShareConfig, - ]; + $script_manager->attachJsToElement($element); return $element; } diff --git a/src/Element/AddThisBasicToolbox.php b/src/Element/AddThisBasicToolbox.php index eee757f..1dc718a 100644 --- a/src/Element/AddThisBasicToolbox.php +++ b/src/Element/AddThisBasicToolbox.php @@ -46,18 +46,8 @@ public function preRender($element) { ); // Add Script. - $element['#attached']['library'][] = 'addthis/addthis.widget'; - $script_manager = \Drupal::getContainer()->get('addthis.script_manager'); - - $addThisConfig = $script_manager->getAddThisConfig(); - $addThisShareConfig = $script_manager->getAddThisShareConfig(); - - $element['#attached']['drupalSettings']['addThisWidget'] = [ - 'widgetScript' => 'http://example.dev/thing.js', - 'config' => $addThisConfig, - 'share' => $addThisShareConfig, - ]; + $script_manager->attachJsToElement($element); $services = trim($element['#services']); $services = str_replace(' ', '', $services); From b4cf33b4cb1e33673ad148efe6a8baabfeee0cce Mon Sep 17 00:00:00 2001 From: Jaleel Carter Date: Fri, 21 Aug 2015 15:03:13 -0400 Subject: [PATCH 49/77] removed bunch of commented code --- src/Element/AddThisBasicToolbox.php | 81 ----------------------------- 1 file changed, 81 deletions(-) diff --git a/src/Element/AddThisBasicToolbox.php b/src/Element/AddThisBasicToolbox.php index 206e170..fd902f5 100644 --- a/src/Element/AddThisBasicToolbox.php +++ b/src/Element/AddThisBasicToolbox.php @@ -34,19 +34,6 @@ public function getInfo() { } public function preRender($element) { -//// $element['addthis_basic_toolbox'] = array( -//// '#type' => 'addthis_wrapper', -//// '#tag' => 'div', -//// '#attributes' => array( -//// 'class' => array( -//// 'addthis_toolbox', -//// 'addthis_default_style', -//// // ($settings['buttons_size'] == AddThis::CSS_32x32 ? AddThis::CSS_32x32 : NULL), -//// //$settings['extra_css'], -//// ), -// ), -// ); - // Add Script. $element['#attached']['library'][] = 'addthis/addthis.widget'; @@ -65,74 +52,6 @@ public function preRender($element) { $services = str_replace(' ', '', $services); $services = explode(',', $services); $element['services'] = $services; -// // All service elements -// $items = array(); -//// foreach ($services as $service) { -//// $items[$service] = array( -//// '#type' => 'addthis_element', -//// '#tag' => 'a', -//// 'value' => '', -//// '#attributes' => array( -//// //'href' => $script_manager->getBaseBookmarkUrl(), -//// 'class' => array( -//// 'addthis_button_' . $service, -//// ), -//// ), -//// '#addthis_service' => $service, -//// ); -//// -//// // Add individual counters. -//// if (strpos($service, 'counter_') === 0) { -//// $items[$service]['#attributes']['class'] = array("addthis_$service"); -//// } -// -// // Basic implementations of bubble counter orientation. -// // @todo Figure all the bubbles out and add them. -// // Still missing: tweetme, hyves and stubleupon, google_plusone_badge. -// // -// $orientation = '';//($settings['counter_orientation'] == 'horizontal' ? TRUE : FALSE); -// switch ($service) { -// case 'linkedin_counter': -// $items[$service]['#attributes'] += array( -// 'li:counter' => ($orientation ? '' : 'top'), -// ); -// break; -// case 'facebook_like': -// $items[$service]['#attributes'] += array( -// 'fb:like:layout' => ($orientation ? 'button_count' : 'box_count') -// ); -// break; -// case 'facebook_share': -// $items[$service]['#attributes'] += array( -// 'fb:share:layout' => ($orientation ? 'button_count' : 'box_count') -// ); -// break; -// case 'google_plusone': -// $items[$service]['#attributes'] += array( -// 'g:plusone:size' => ($orientation ? 'standard' : 'tall') -// ); -// break; -// case 'tweet': -// $items[$service]['#attributes'] += array( -// 'tw:count' => ($orientation ? 'horizontal' : 'vertical'), -// 'tw:via' => $script_manager->getTwitterVia(), -// ); -// break; -// case 'bubble_style': -// $items[$service]['#attributes']['class'] = array( -// 'addthis_counter', -// 'addthis_bubble_style' -// ); -// break; -// case 'pill_style': -// $items[$service]['#attributes']['class'] = array( -// 'addthis_counter', -// 'addthis_pill_style' -// ); -// break; -// } -// } - return $element; } From ce4e80b06c0616c1a29afada621d259188b9bb2e Mon Sep 17 00:00:00 2001 From: Jaleel Carter Date: Fri, 21 Aug 2015 15:05:00 -0400 Subject: [PATCH 50/77] removed more commented code --- templates/addthis-basic-toolbox.html.twig | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/templates/addthis-basic-toolbox.html.twig b/templates/addthis-basic-toolbox.html.twig index 23ce0d9..8894929 100644 --- a/templates/addthis-basic-toolbox.html.twig +++ b/templates/addthis-basic-toolbox.html.twig @@ -9,18 +9,7 @@ #}
-{#{{ element }}#} {% for service in element.services %} {% endfor %}
- - -{## print all variables#} -{#{{ dump(_context) }}#} -{## print only keys#} -{#{{ dump(_context|keys) }}#} -{## foreach loop for myarray#} -{#{% for service in element.services %}#} -{##} -{#{% endfor %}#} \ No newline at end of file From 2e3040ea8155a47832ccb3a269e41fa92d18d20a Mon Sep 17 00:00:00 2001 From: Jaleel Carter Date: Fri, 21 Aug 2015 15:14:25 -0400 Subject: [PATCH 51/77] removed $config variable --- .../Field/FieldFormatter/AddThisBasicButtonFormatter.php | 6 +++--- .../Field/FieldFormatter/AddThisBasicToolboxFormatter.php | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php index 6447858..3f60441 100644 --- a/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php @@ -52,11 +52,11 @@ public function settingsForm(array $form, FormStateInterface $form_state) { * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items) { - + $settings = $this->getSettings(); return array( '#type' => 'addthis_basic_button', - '#size' => $config['basic_button']['button_size'], - '#extra_classes' => $config['basic_button']['extra_css'], + '#size' => $settings['basic_button']['button_size'], + '#extra_classes' => $settings['basic_button']['extra_css'], ); } diff --git a/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php index 2b8fb56..bfe2e6d 100644 --- a/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php @@ -53,12 +53,12 @@ public function settingsForm(array $form, FormStateInterface $form_state) { * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items) { - + $settings = $this->getSettings(); return array( '#type' => 'addthis_basic_toolbox', - '#size' => $config['basic_toolbox']['buttons_size'], - '#services' => $config['basic_toolbox']['share_services'], - '#extra_classes' => $config['basic_toolbox']['extra_css'], + '#size' => $settings['basic_toolbox']['buttons_size'], + '#services' => $settings['basic_toolbox']['share_services'], + '#extra_classes' => $settings['basic_toolbox']['extra_css'], ); } From 232fa3b31317daf8af70aa9db5ef9e8cec500bd8 Mon Sep 17 00:00:00 2001 From: Jaleel Carter Date: Fri, 21 Aug 2015 15:58:40 -0400 Subject: [PATCH 52/77] fixed missing class not showing on button --- templates/addthis-basic-button.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/addthis-basic-button.html.twig b/templates/addthis-basic-button.html.twig index bb3071e..575196f 100644 --- a/templates/addthis-basic-button.html.twig +++ b/templates/addthis-basic-button.html.twig @@ -10,6 +10,6 @@ - + {{ element }} From 77a3fe610b0ac3ad3642528ae4f0ed39ca894d19 Mon Sep 17 00:00:00 2001 From: Jaleel Carter Date: Fri, 21 Aug 2015 16:28:29 -0400 Subject: [PATCH 53/77] fix for field formatters. --- .../Field/FieldFormatter/AddThisBasicButtonFormatter.php | 4 ++-- .../Field/FieldFormatter/AddThisBasicToolboxFormatter.php | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php index 3f60441..c28dd61 100644 --- a/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php @@ -55,8 +55,8 @@ public function viewElements(FieldItemListInterface $items) { $settings = $this->getSettings(); return array( '#type' => 'addthis_basic_button', - '#size' => $settings['basic_button']['button_size'], - '#extra_classes' => $settings['basic_button']['extra_css'], + '#size' => $settings[settings][addthis_settings][type_settings][basic_button][button_size], + '#extra_classes' => $settings[settings][addthis_settings][type_settings][basic_button][extra_css], ); } diff --git a/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php index bfe2e6d..9bfdbcc 100644 --- a/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php @@ -56,9 +56,9 @@ public function viewElements(FieldItemListInterface $items) { $settings = $this->getSettings(); return array( '#type' => 'addthis_basic_toolbox', - '#size' => $settings['basic_toolbox']['buttons_size'], - '#services' => $settings['basic_toolbox']['share_services'], - '#extra_classes' => $settings['basic_toolbox']['extra_css'], + '#size' => $settings[settings][addthis_settings][type_settings][basic_toolbox][buttons_size], + '#services' => $settings[settings][addthis_settings][type_settings][basic_toolbox][share_services], + '#extra_classes' => $settings[settings][addthis_settings][type_settings][basic_toolbox][extra_css], ); } From 74e5648f1aaa4c5784f607af05807488c71bd048 Mon Sep 17 00:00:00 2001 From: Jaleel Carter Date: Fri, 21 Aug 2015 16:58:25 -0400 Subject: [PATCH 54/77] fixed field formatters --- .../Field/FieldFormatter/AddThisBasicButtonFormatter.php | 6 ++---- .../Field/FieldFormatter/AddThisBasicToolboxFormatter.php | 6 +++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php index c28dd61..8e9a983 100644 --- a/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php @@ -55,10 +55,8 @@ public function viewElements(FieldItemListInterface $items) { $settings = $this->getSettings(); return array( '#type' => 'addthis_basic_button', - '#size' => $settings[settings][addthis_settings][type_settings][basic_button][button_size], - '#extra_classes' => $settings[settings][addthis_settings][type_settings][basic_button][extra_css], + '#size' => $settings['button_size'], + '#extra_classes' => $settings['extra_css'], ); } - - } \ No newline at end of file diff --git a/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php index 9bfdbcc..965333d 100644 --- a/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php @@ -56,9 +56,9 @@ public function viewElements(FieldItemListInterface $items) { $settings = $this->getSettings(); return array( '#type' => 'addthis_basic_toolbox', - '#size' => $settings[settings][addthis_settings][type_settings][basic_toolbox][buttons_size], - '#services' => $settings[settings][addthis_settings][type_settings][basic_toolbox][share_services], - '#extra_classes' => $settings[settings][addthis_settings][type_settings][basic_toolbox][extra_css], + '#size' => $settings['buttons_size'], + '#services' => $settings['share_services'], + '#extra_classes' => $settings['extra_css'], ); } From 3919e5d0653747b60d4326466c44aa98cebcb640 Mon Sep 17 00:00:00 2001 From: Jason Enter Date: Fri, 21 Aug 2015 17:33:25 -0400 Subject: [PATCH 55/77] saving Addthis JS settings / config progress --- js/addthis.js | 18 +++------- src/AddThisBasicToolboxFormTrait.php | 2 +- src/AddThisScriptManager.php | 49 ++++++++++++++++++---------- 3 files changed, 38 insertions(+), 31 deletions(-) diff --git a/js/addthis.js b/js/addthis.js index cff8dff..a042ee1 100644 --- a/js/addthis.js +++ b/js/addthis.js @@ -1,4 +1,5 @@ -// AddThis requires a global variable. :( +// AddThis requires a global variable + var addthis_config, addthis_share; @@ -7,24 +8,15 @@ var addthis_config, attach: function (context, settings) { if (context === document) { // only fires on document load - console.log(settings.addThisWidget); - - console.log('loaded '); - - // Because we cannot dynamically add JS, we do it here. :( + // Because we cannot dynamically add JS if (typeof settings.addThisWidget.widgetScript !== 'undefined') { $.getScript(settings.addThisWidget.widgetScript) .done(function (script, textStatus) { - // console.log(settings.addThisWidget.widgetScript); - - console.log('Success'); - + // console.log('Success'); }).fail(function (jqxhr, settings, exception) { - // TODO: check for fail msg - console.log('Fail'); - + // console.log('Fail'); }); } diff --git a/src/AddThisBasicToolboxFormTrait.php b/src/AddThisBasicToolboxFormTrait.php index ae2645c..4b47190 100644 --- a/src/AddThisBasicToolboxFormTrait.php +++ b/src/AddThisBasicToolboxFormTrait.php @@ -94,7 +94,7 @@ protected function addThisBasicToolboxForm($options){ * @param array $element * @param FormStateInterface $form_state */ - public static function addThisDisplayElementServicesValidate($element, FormStateInterface $form_state) { + public static function addThisDisplayElementServicesValidate($element, $form_state) { $bad = FALSE; $services = trim($element['#value']); diff --git a/src/AddThisScriptManager.php b/src/AddThisScriptManager.php index 861e684..5b1c407 100644 --- a/src/AddThisScriptManager.php +++ b/src/AddThisScriptManager.php @@ -142,37 +142,52 @@ protected function getServiceNamesAsCommaSeparatedString($services) { */ public function attachJsToElement(&$element) { - $addthis_url_configs = array(); + $config = $this->config_factory->get('addthis.settings'); $adv_config = $this->config_factory->get('addthis.settings.advanced'); - if ($adv_config->get('addthis_widget_js_include') !== 0) { + $menu_style = $config->get('compact_menu.menu_style'); + + $addthis_settings = array(); + + foreach($menu_style as $key => $val){ + $c_key = str_replace('addthis_', '', $key); + $addthis_settings['menu_style'][$c_key] = $val; + } + + // TODO: other section under main settings - $widget_js_url = $adv_config->get('addthis_widget_js_url'); - $pubid = $config->get('analytics.addthis_profile_id'); + // $pubid = $config->get('analytics.addthis_profile_id'); - if (isset($pubid) && !empty($pubid) && is_string($pubid)) { + /* + if (isset($pubid) && !empty($pubid) && is_string($pubid)) { //$widget_js->addAttribute('pubid', $pubid); } - $async = $this->async; - if ($async) { - //$widget_js->addAttribute('async', 1); - } - if ($this->domready) { - //$widget_js->addAttribute('domready', 1); - } - } + */ + + /* + * AddThis Advanced Settings + */ + $addthis_adv_settings = array(); + $addthis_adv_settings['bookmark_url'] = $adv_config->get('addthis_bookmark_url'); + $addthis_adv_settings['services_css_url'] = $adv_config->get('addthis_services_css_url'); + $addthis_adv_settings['services_json_url'] = $adv_config->get('addthis_services_json_url'); + $addthis_adv_settings['widget_js_url'] = $adv_config->get('addthis_widget_js_url'); + $addthis_adv_settings['conf_code_enabled'] = $adv_config->get('addthis_custom_configuration_code_enabled'); + $addthis_adv_settings['conf_code'] = $adv_config->get('addthis_custom_configuration_code'); + $addthis_adv_settings['load_domready'] = $adv_config->get('addthis_widget_load_domready'); + $addthis_adv_settings['load_async'] = $adv_config->get('addthis_widget_load_async'); + $addthis_adv_settings['widget_include'] = $adv_config->get('addthis_widget_include'); + $addthis_adv_settings['css_url_key'] = $adv_config->get('addthis_services_css_url_key'); + $addthis_adv_settings['json_url_key'] = $adv_config->get('addthis_services_json_url_key'); - if(!isset($widget_js_url)) { - $widget_js_url = 'http://s7.addthis.com/js/250/addthis_widget.js'; - } $element['#attached']['library'][] = 'addthis/addthis.widget'; $addThisConfig = $this->getAddThisConfig(); $addThisShareConfig = $this->getAddThisShareConfig(); $element['#attached']['drupalSettings']['addThisWidget'] = [ - 'widgetScript' => $widget_js_url, + 'widgetScript' => $addthis_adv_settings['widget_js_url'], 'config' => $addThisConfig, 'share' => $addThisShareConfig, ]; From 53334a26d1f627209b88ad38a922362aec1ab439 Mon Sep 17 00:00:00 2001 From: Jason Enter Date: Mon, 24 Aug 2015 09:25:25 -0400 Subject: [PATCH 56/77] cleaning up old code --- addthis.libraries.yml | 1 - js/addthis.js | 10 ---------- 2 files changed, 11 deletions(-) diff --git a/addthis.libraries.yml b/addthis.libraries.yml index b6cba99..2b0ff77 100644 --- a/addthis.libraries.yml +++ b/addthis.libraries.yml @@ -8,7 +8,6 @@ addthis.admin: addthis.widget: version: VERSION js: - # 'http://s7.addthis.com/js/300/addthis_widget.js': {type: external} js/addthis.js: {} dependencies: - core/jquery diff --git a/js/addthis.js b/js/addthis.js index a042ee1..e903326 100644 --- a/js/addthis.js +++ b/js/addthis.js @@ -12,21 +12,11 @@ var addthis_config, if (typeof settings.addThisWidget.widgetScript !== 'undefined') { $.getScript(settings.addThisWidget.widgetScript) .done(function (script, textStatus) { - // console.log(settings.addThisWidget.widgetScript); - // console.log('Success'); }).fail(function (jqxhr, settings, exception) { // TODO: check for fail msg - // console.log('Fail'); }); } - /* - console.log(addthis_share); - console.log('- - - - - - - - - - - - '); - console.log(settings.addThisWidget); - console.log('- - - - - - - - - - - - '); - */ - } } }; From 74773b6e4a519d5309d06c7142eb931a6c8f234e Mon Sep 17 00:00:00 2001 From: John Doyle Date: Tue, 25 Aug 2015 11:54:16 -0400 Subject: [PATCH 57/77] Updated AddThisScriptManager to dynamically build the widget_js URL based on configurable paramaters: pubid, domready, async. Connect with Jason to ensure we do the loading properly based on the settings --- js/addthis.js | 3 + src/AddThisScriptManager.php | 129 ++++++++++++++++------------------- 2 files changed, 63 insertions(+), 69 deletions(-) diff --git a/js/addthis.js b/js/addthis.js index e903326..fce326d 100644 --- a/js/addthis.js +++ b/js/addthis.js @@ -5,6 +5,9 @@ var addthis_config, (function ($, Drupal, window, document, undefined) { Drupal.behaviors.addThisWidget = { + + //@TODO Need to support domready and async loading. See http://support.addthis.com/customer/portal/articles/1338006-url-parameters + attach: function (context, settings) { if (context === document) { // only fires on document load diff --git a/src/AddThisScriptManager.php b/src/AddThisScriptManager.php index 5b1c407..8fc0019 100644 --- a/src/AddThisScriptManager.php +++ b/src/AddThisScriptManager.php @@ -19,6 +19,8 @@ namespace Drupal\addthis; +use Drupal\Core\Url; + class AddThisScriptManager { /** * @var \Drupal\Core\Language\LanguageManager @@ -42,9 +44,11 @@ public function __construct(\Drupal\Core\Language\LanguageManager $languageManag } /** - * Get a array with all addthis_config values. + * Set values for addthis_config based on Sharing API documentation. See + * http://support.addthis.com/customer/portal/articles/1337994-the-addthis_config-variable + * for more details. * - * Allow alter through 'addthis_configuration'. + * @TODO Allow alter of 'addthis_configuration'. * * @todo Add static cache. * @@ -58,10 +62,9 @@ public function getAddThisConfig() { $excluded_services = $this->getServiceNamesAsCommaSeparatedString($config->get('excluded_services.addthis_excluded_services')); $configuration = [ - 'pubid' => $config->get('analytics.addthis_profile_id'), 'services_compact' => $enabled_services, 'services_exclude' => $excluded_services, - 'data_track_clickback' => $config->get('analytics.addthis_clickback_tracking_enabled'), + //'services_expanded' => @todo - add this 'ui_508_compliant' => $config->get('compact_menu.additionals.addthis_508_compliant'), 'ui_click' => $config->get('compact_menu.menu_style.addthis_click_to_open_compact_menu_enabled'), 'ui_cobrand' => $config->get('compact_menu.menu_style.addthis_co_brand'), @@ -72,23 +75,28 @@ public function getAddThisConfig() { 'ui_use_css' => $config->get('compact_menu.additionals.addthis_standard_css_enabled'), 'ui_use_addressbook' => $config->get('compact_menu.additionals.addthis_addressbook_enabled'), 'ui_language' => $this->language_manager->getCurrentLanguage()->getId(), + 'pubid' => $config->get('analytics.addthis_profile_id'), + 'data_track_clickback' => $config->get('analytics.addthis_clickback_tracking_enabled'), + ]; - // TODO: Do we need to check if the module exists or can we just check the setting? + + //Ensure that the Google Analytics module is enabled for tracking. if (\Drupal::moduleHandler()->moduleExists('googleanalytics')) { if ($config->get('analytics.addthis_google_analytics_tracking_enabled')) { - $configuration['data_ga_property'] = $this->config_factory->get('google_analytics.settings')->get('google_analytics_account'); + $configuration['data_ga_property'] = $this->config_factory->get('google_analytics.settings') + ->get('google_analytics_account'); $configuration['data_ga_social'] = $config->get('analytics.addthis_google_analytics_social_tracking_enabled'); } } - // drupal_alter('addthis_configuration', $configuration); return $configuration; } /** - * Get a array with all addthis_share values. + * Get a array with all addthis_share values that we set. More documentation can + * be found here: http://support.addthis.com/customer/portal/articles/1337996-the-addthis_share-variable * - * Allow alter through 'addthis_configuration_share'. + * @TODO Allow alter of 'addthis_configuration_share'. * * @todo Add static cache. * @@ -96,18 +104,10 @@ public function getAddThisConfig() { * SRP is lost here. */ public function getAddThisShareConfig() { + $config = $this->config_factory->get('addthis.settings'); - $configuration = $this->getAddThisConfig(); - - if (isset($configuration['templates'])) { - $addthis_share = [ - 'templates' => $configuration['templates'], - ]; - } - $addthis_share['templates']['twitter'] = $this->config_factory->get('addthis.settings') - ->get('third_party.addthis_twitter_template'); + $addthis_share['templates']['twitter'] = $config->get('third_party.addthis_twitter_template'); - //drupal_alter('addthis_configuration_share', $configuration); return $addthis_share; } @@ -130,70 +130,61 @@ protected function getServiceNamesAsCommaSeparatedString($services) { /** - * Attach the widget js to the element. - * - * @todo Change the scope of the addthis.js. - * See if we can get the scope of the addthis.js into the header - * just below the settings so that the settings can be used in the loaded - * addthis.js of our module. - * - * @param array $element - * The element to attach the JavaScript to. - */ - public function attachJsToElement(&$element) { + * Attach the widget js to the element. + * + * @todo Change the scope of the addthis.js. + * See if we can get the scope of the addthis.js into the header + * just below the settings so that the settings can be used in the loaded + * addthis.js of our module. + * + * @param array $element + * The element to attach the JavaScript to. + */ + public function attachJsToElement(&$element) { + $config = $this->config_factory->get('addthis.settings'); + $adv_config = $this->config_factory->get('addthis.settings.advanced'); + //Generate AddThisWidgetURL + $fragment = []; - $config = $this->config_factory->get('addthis.settings'); - $adv_config = $this->config_factory->get('addthis.settings.advanced'); + $pubid = $config->get('analytics.addthis_profile_id'); + if (isset($pubid) && !empty($pubid) && is_string($pubid)) { + $fragment[] = 'pubid=' . $pubid; + } - $menu_style = $config->get('compact_menu.menu_style'); + if ($adv_config->get('addthis_widget_load_async')) { + $fragment[] = 'async=1'; + } - $addthis_settings = array(); + if ($adv_config->get('addthis_widget_load_domready')) { + $fragment[] = 'domready=1'; + } - foreach($menu_style as $key => $val){ - $c_key = str_replace('addthis_', '', $key); - $addthis_settings['menu_style'][$c_key] = $val; - } - // TODO: other section under main settings + $element['#attached']['library'][] = 'addthis/addthis.widget'; + $addThisConfig = $this->getAddThisConfig(); + $addThisShareConfig = $this->getAddThisShareConfig(); - // $pubid = $config->get('analytics.addthis_profile_id'); - /* - if (isset($pubid) && !empty($pubid) && is_string($pubid)) { - //$widget_js->addAttribute('pubid', $pubid); - } - */ - /* - * AddThis Advanced Settings - */ - $addthis_adv_settings = array(); - $addthis_adv_settings['bookmark_url'] = $adv_config->get('addthis_bookmark_url'); - $addthis_adv_settings['services_css_url'] = $adv_config->get('addthis_services_css_url'); - $addthis_adv_settings['services_json_url'] = $adv_config->get('addthis_services_json_url'); - $addthis_adv_settings['widget_js_url'] = $adv_config->get('addthis_widget_js_url'); - $addthis_adv_settings['conf_code_enabled'] = $adv_config->get('addthis_custom_configuration_code_enabled'); - $addthis_adv_settings['conf_code'] = $adv_config->get('addthis_custom_configuration_code'); - $addthis_adv_settings['load_domready'] = $adv_config->get('addthis_widget_load_domready'); - $addthis_adv_settings['load_async'] = $adv_config->get('addthis_widget_load_async'); - $addthis_adv_settings['widget_include'] = $adv_config->get('addthis_widget_include'); - $addthis_adv_settings['css_url_key'] = $adv_config->get('addthis_services_css_url_key'); - $addthis_adv_settings['json_url_key'] = $adv_config->get('addthis_services_json_url_key'); + $options = [ + 'fragment' => implode('&', $fragment), + 'external' => TRUE, + ]; - $element['#attached']['library'][] = 'addthis/addthis.widget'; - $addThisConfig = $this->getAddThisConfig(); - $addThisShareConfig = $this->getAddThisShareConfig(); + $widget_url = $adv_config->get('addthis_widget_js_url'); + $widgetURL = URL::fromUri($widget_url, $options)->toString(); - $element['#attached']['drupalSettings']['addThisWidget'] = [ - 'widgetScript' => $addthis_adv_settings['widget_js_url'], - 'config' => $addThisConfig, - 'share' => $addThisShareConfig, - ]; + $element['#attached']['drupalSettings']['addThisWidget'] = [ + 'widgetScript' => $widgetURL, + 'config' => $addThisConfig, + 'share' => $addThisShareConfig, + ]; - } + + } } From 80a94dfa203fe71161ce9ad0e2ef57d2c93960c4 Mon Sep 17 00:00:00 2001 From: John Doyle Date: Tue, 25 Aug 2015 12:47:47 -0400 Subject: [PATCH 58/77] Resolved Google Analytics integration syntax. Still need to test and verify config is getting loaded and passed to AddThis properly. --- src/AddThisScriptManager.php | 4 +-- src/Form/AddThisSettingsForm.php | 43 +++++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/AddThisScriptManager.php b/src/AddThisScriptManager.php index 8fc0019..f49e552 100644 --- a/src/AddThisScriptManager.php +++ b/src/AddThisScriptManager.php @@ -81,10 +81,10 @@ public function getAddThisConfig() { ]; //Ensure that the Google Analytics module is enabled for tracking. - if (\Drupal::moduleHandler()->moduleExists('googleanalytics')) { + if (\Drupal::moduleHandler()->moduleExists('google_analytics')) { if ($config->get('analytics.addthis_google_analytics_tracking_enabled')) { $configuration['data_ga_property'] = $this->config_factory->get('google_analytics.settings') - ->get('google_analytics_account'); + ->get('account'); $configuration['data_ga_social'] = $config->get('analytics.addthis_google_analytics_social_tracking_enabled'); } } diff --git a/src/Form/AddThisSettingsForm.php b/src/Form/AddThisSettingsForm.php index a48816e..6aadad4 100644 --- a/src/Form/AddThisSettingsForm.php +++ b/src/Form/AddThisSettingsForm.php @@ -18,6 +18,22 @@ */ class AddThisSettingsForm extends ConfigFormBase { + /** + * @var \Drupal\Core\Config\ConfigFactory + */ + protected $config_factory; + + /** + * Construct function. + * + * @param \Drupal\Core\Language\LanguageManager $languageManager + * @param \Drupal\Core\Config\ConfigFactory $configFactory + */ + public function __construct(\Drupal\Core\Config\ConfigFactory $configFactory) { + $this->config_factory = $configFactory; + } + + /** * {@inheritdoc} */ @@ -236,28 +252,31 @@ public function buildForm(array $form, FormStateInterface $form_state) { // Google Analytics and Google Social Tracking support. $can_do_google_social_tracking = \Drupal::moduleHandler() - ->moduleExists('googleanalytics'); + ->moduleExists('google_analytics'); //@TODO Get back to this. - $google_analytics_config = \Drupal::config(google_analytics . settings); - $google_analytics_account = $google_analytics_config->get('google_analytics_account'); + $google_analytics_config = $this->config_factory->get('google_analytics.settings'); + $google_analytics_account = $google_analytics_config->get('account'); $is_google_analytics_setup = $can_do_google_social_tracking && isset($google_analytics_account); $form['details_analytics']['google_analytics'] = array( - '#theme' => 'html_tag', - '#tag' => 'div', - '#value' => '' . t('Google Analytics') . '', + '#type' => 'markup', + '#prefix' => '
', + '#suffix' => '
', + '#markup' => '' . t('Google Analytics') . '', ); if (!$can_do_google_social_tracking) { $form['details_analytics']['can_do_google_analytics'] = array( - '#theme' => 'html_tag', - '#tag' => 'p', - '#value' => '' . t('Install/enable the Google Analytics module for Social Tracking support.') . '', + '#type' => 'markup', + '#prefix' => '

', + '#suffix' => '

', + '#markup' => '' . t('Install/enable the Google Analytics module for Social Tracking support.') . '', ); } elseif ($can_do_google_social_tracking && !$is_google_analytics_setup) { $form['details_analytics']['can_do_google_analytics'] = array( - '#theme' => 'html_tag', - '#tag' => 'p', - '#value' => '' . t('Configure the Google Analytics module correctly with the account code to use this feature.') . '', + '#type' => 'markup', + '#prefix' => '

', + '#suffix' => '

', + '#markup' => '' . t('Configure the Google Analytics module correctly with the account code to use this feature.') . '', ); } $form['details_analytics']['addthis_google_analytics_tracking_enabled'] = array( From 0fff92489f564707260059946ed37625cd9fafc6 Mon Sep 17 00:00:00 2001 From: John Doyle Date: Tue, 25 Aug 2015 12:52:26 -0400 Subject: [PATCH 59/77] Set proper addthis settings in the JS file. --- js/addthis.js | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/js/addthis.js b/js/addthis.js index fce326d..2243ba2 100644 --- a/js/addthis.js +++ b/js/addthis.js @@ -1,27 +1,32 @@ // AddThis requires a global variable var addthis_config, - addthis_share; + addthis_share; (function ($, Drupal, window, document, undefined) { - Drupal.behaviors.addThisWidget = { + Drupal.behaviors.addThisWidget = { - //@TODO Need to support domready and async loading. See http://support.addthis.com/customer/portal/articles/1338006-url-parameters + //@TODO Need to support domready and async loading. See http://support.addthis.com/customer/portal/articles/1338006-url-parameters - attach: function (context, settings) { - if (context === document) { // only fires on document load + attach: function (context, settings) { + if (context === document) { // only fires on document load - // Because we cannot dynamically add JS - if (typeof settings.addThisWidget.widgetScript !== 'undefined') { - $.getScript(settings.addThisWidget.widgetScript) - .done(function (script, textStatus) { - }).fail(function (jqxhr, settings, exception) { - // TODO: check for fail msg - }); + // Because we cannot dynamically add JS + if (typeof settings.addThisWidget.widgetScript !== 'undefined') { + $.getScript(settings.addThisWidget.widgetScript) + .done(function (script, textStatus) { + }).fail(function (jqxhr, settings, exception) { + // TODO: check for fail msg + }); + } + if (typeof settings.addThisWidget.config !== 'undefined') { + addthis_config = settings.addThisWidget.config; + } + if (typeof settings.addThisWidget.share !== 'undefined') { + addthis_share = settings.addThisWidget.config; + } + } } - - } - } - }; + }; })(jQuery, Drupal, this, this.document); From b04fb7605f2886bb306c608894125d0951e5bcec Mon Sep 17 00:00:00 2001 From: John Doyle Date: Tue, 25 Aug 2015 13:36:27 -0400 Subject: [PATCH 60/77] Always load addthis on domready. Remove the config and logic to look for domready. --- src/AddThisScriptManager.php | 4 ---- src/Form/AddThisSettingsAdvancedForm.php | 6 ------ 2 files changed, 10 deletions(-) diff --git a/src/AddThisScriptManager.php b/src/AddThisScriptManager.php index f49e552..23b05fd 100644 --- a/src/AddThisScriptManager.php +++ b/src/AddThisScriptManager.php @@ -156,10 +156,6 @@ public function attachJsToElement(&$element) { $fragment[] = 'async=1'; } - if ($adv_config->get('addthis_widget_load_domready')) { - $fragment[] = 'domready=1'; - } - $element['#attached']['library'][] = 'addthis/addthis.widget'; $addThisConfig = $this->getAddThisConfig(); diff --git a/src/Form/AddThisSettingsAdvancedForm.php b/src/Form/AddThisSettingsAdvancedForm.php index 3581755..2a98785 100644 --- a/src/Form/AddThisSettingsAdvancedForm.php +++ b/src/Form/AddThisSettingsAdvancedForm.php @@ -93,11 +93,6 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#required' => FALSE, '#description' => t('AddThis custom configuration code. See format at AddThis.com'), ); - $form['advanced_settings_details']['addthis_widget_load_domready'] = array( - '#type' => 'checkbox', - '#title' => t('Load the AddThis resources after the DOM is ready.'), - '#default_value' => $config->get('addthis_widget_load_domready'), - ); $form['advanced_settings_details']['addthis_widget_load_async'] = array( '#type' => 'checkbox', '#title' => t('Initialize asynchronously through addthis.init().'), @@ -130,7 +125,6 @@ public function submitForm(array &$form, FormStateInterface $form_state) { ->set('addthis_widget_js_url', $form_state->getValue('addthis_widget_js_url')) ->set('addthis_custom_configuration_code_enabled', $form_state->getValue('addthis_custom_configuration_code_enabled')) ->set('addthis_custom_configuration_code', $form_state->getValue('addthis_custom_configuration_code')) - ->set('addthis_widget_load_domready', $form_state->getValue('addthis_widget_load_domready')) ->set('addthis_widget_load_async', $form_state->getValue('addthis_widget_load_async')) ->set('addthis_widget_include', $form_state->getValue('addthis_widget_include')) ->save(); From fe87fc9015f871b8024575ea9e7411933ef0137b Mon Sep 17 00:00:00 2001 From: John Doyle Date: Tue, 25 Aug 2015 13:36:38 -0400 Subject: [PATCH 61/77] always load domready into url --- src/AddThisScriptManager.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/AddThisScriptManager.php b/src/AddThisScriptManager.php index 23b05fd..4a3351c 100644 --- a/src/AddThisScriptManager.php +++ b/src/AddThisScriptManager.php @@ -156,6 +156,9 @@ public function attachJsToElement(&$element) { $fragment[] = 'async=1'; } + //Always load the script with domready flag. + $fragment[] = 'domready=1'; + $element['#attached']['library'][] = 'addthis/addthis.widget'; $addThisConfig = $this->getAddThisConfig(); From 124c2736551d25becb918bc926b91152070a3eb7 Mon Sep 17 00:00:00 2001 From: John Doyle Date: Tue, 25 Aug 2015 14:04:35 -0400 Subject: [PATCH 62/77] Started implementation for orientation and counters. Jason will finish --- src/Element/AddThisBasicToolbox.php | 11 +++++++++++ templates/addthis-basic-toolbox.html.twig | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Element/AddThisBasicToolbox.php b/src/Element/AddThisBasicToolbox.php index 0454fad..5ff58d8 100644 --- a/src/Element/AddThisBasicToolbox.php +++ b/src/Element/AddThisBasicToolbox.php @@ -43,6 +43,17 @@ public function preRender($element) { $services = explode(',', $services); $element['services'] = $services; + + foreach ($services as $key => $service){ + $element['services'][$key] = array(); + $element['services'][$key]['service'] = $service; + switch ($service) { + case 'linkedin_counter': + $element['services'][$key]['attributes'] = new Attribute(array('li:counter' => 'top')); + break; + } + } + return $element; } diff --git a/templates/addthis-basic-toolbox.html.twig b/templates/addthis-basic-toolbox.html.twig index 8894929..13ae9b0 100644 --- a/templates/addthis-basic-toolbox.html.twig +++ b/templates/addthis-basic-toolbox.html.twig @@ -10,6 +10,6 @@
{% for service in element.services %} - + {% endfor %}
From 31f13131c18a822c8fb4580780429b8f0e57c26b Mon Sep 17 00:00:00 2001 From: Jason Enter Date: Tue, 25 Aug 2015 16:34:17 -0400 Subject: [PATCH 63/77] update style config ports --- src/Element/AddThisBasicButton.php | 4 +-- src/Element/AddThisBasicToolbox.php | 47 +++++++++++++++++++++++++---- src/Plugin/Block/AddThisBlock.php | 1 + 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/Element/AddThisBasicButton.php b/src/Element/AddThisBasicButton.php index 48b6b6f..c15c075 100644 --- a/src/Element/AddThisBasicButton.php +++ b/src/Element/AddThisBasicButton.php @@ -24,8 +24,8 @@ public function getInfo() { $config = $this->configuration; return [ '#theme' => 'addthis_basic_button', - '#size' => $config['basic_button']['button_size'], - '#extra_classes' => $config['basic_button']['extra_css'], + '#size' => 'addthis_16x16_style', + '#extra_classes' => '', '#pre_render' => [ [$class, 'preRender'], ], diff --git a/src/Element/AddThisBasicToolbox.php b/src/Element/AddThisBasicToolbox.php index 5ff58d8..f3f25f4 100644 --- a/src/Element/AddThisBasicToolbox.php +++ b/src/Element/AddThisBasicToolbox.php @@ -24,15 +24,17 @@ public function getInfo() { $config = $this->configuration; return [ '#theme' => 'addthis_basic_toolbox', - '#size' => $config['basic_toolbox']['buttons_size'], - '#services' => $config['basic_toolbox']['share_services'], - '#extra_classes' => $config['basic_toolbox']['extra_css'], + '#size' => 'addthis_16x16_style', + '#services' => 'facebook,twitter', + '#extra_classes' => '', + '#counter_orientation' => 'horizontal', '#pre_render' => [ [$class, 'preRender'], ], ]; } + public function preRender($element) { // Add Script. $script_manager = \Drupal::getContainer()->get('addthis.script_manager'); @@ -41,19 +43,52 @@ public function preRender($element) { $services = trim($element['#services']); $services = str_replace(' ', '', $services); $services = explode(',', $services); - $element['services'] = $services; + // Orientation + if ($element['#counter_orientation'] == 'vertical') { + $isvertical = TRUE; + } - foreach ($services as $key => $service){ + foreach ($services as $key => $service) { $element['services'][$key] = array(); $element['services'][$key]['service'] = $service; switch ($service) { case 'linkedin_counter': - $element['services'][$key]['attributes'] = new Attribute(array('li:counter' => 'top')); + $element['services'][$key]['attributes'] = new Attribute(array('li:counter' => ($isvertical ? 'top' : ''))); + break; + case 'facebook_like': + $element['services'][$key]['attributes'] = new Attribute(array('fb:like:layout' => ($isvertical ? 'box_count' : 'button_count'))); + break; + case 'facebook_share': + $element['services'][$key]['attributes'] = new Attribute(array('fb:share:layout' => ($isvertical ? 'box_count' : 'button_count'))); + break; + case 'google_plusone': + $element['services'][$key]['attributes'] = new Attribute(array('g:plusone:size' => ($isvertical ? 'tall' : 'standard'))); break; + case 'tweet': + $element['services'][$key]['attributes'] = new Attribute(array( + 'tw:count' => ($isvertical ? 'vertical' : 'horizontal'), + 'tw:via' => '' // TODO: D7 used AddThis::getInstance()->getTwitterVia() + )); + break; + /* + * case 'bubble_style': + $element['services'][$key]['attributes'] = new Attribute(array( + 'addthis_counter', + 'addthis_bubble_style' + )); + break; + case 'pill_style': + $element['services'][$key]['attributes'] = new Attribute(array( + 'addthis_counter', + 'addthis_pill_style' + )); + break; + */ } } + return $element; } diff --git a/src/Plugin/Block/AddThisBlock.php b/src/Plugin/Block/AddThisBlock.php index 066d14f..ae770a0 100644 --- a/src/Plugin/Block/AddThisBlock.php +++ b/src/Plugin/Block/AddThisBlock.php @@ -169,6 +169,7 @@ public function build() { '#size' => $config['basic_toolbox']['buttons_size'], '#services' => $config['basic_toolbox']['share_services'], '#extra_classes' => $config['basic_toolbox']['extra_css'], + '#counter_orientation' => $config['basic_toolbox']['counter_orientation'], ]; break; } From 45465e49fa3c41c3939dbc25ff41fce6ea2fe9be Mon Sep 17 00:00:00 2001 From: Jason Enter Date: Tue, 25 Aug 2015 17:47:48 -0400 Subject: [PATCH 64/77] class updates on twig vars --- src/Element/AddThisBasicToolbox.php | 36 +++++++++++------------ templates/addthis-basic-toolbox.html.twig | 3 +- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/Element/AddThisBasicToolbox.php b/src/Element/AddThisBasicToolbox.php index f3f25f4..e33ce74 100644 --- a/src/Element/AddThisBasicToolbox.php +++ b/src/Element/AddThisBasicToolbox.php @@ -52,40 +52,38 @@ public function preRender($element) { foreach ($services as $key => $service) { $element['services'][$key] = array(); $element['services'][$key]['service'] = $service; + $attributes = [ + 'class' => ['addthis_button_' . $service] + ]; switch ($service) { case 'linkedin_counter': - $element['services'][$key]['attributes'] = new Attribute(array('li:counter' => ($isvertical ? 'top' : ''))); + $attributes['li:counter'] = $isvertical ? 'top' : ''; break; case 'facebook_like': - $element['services'][$key]['attributes'] = new Attribute(array('fb:like:layout' => ($isvertical ? 'box_count' : 'button_count'))); + $attributes['fb:like:layout'] = $isvertical ? 'box_count' : 'button_count'; break; case 'facebook_share': - $element['services'][$key]['attributes'] = new Attribute(array('fb:share:layout' => ($isvertical ? 'box_count' : 'button_count'))); + $attributes['fb:share:layout'] = $isvertical ? 'box_count' : 'button_count'; break; case 'google_plusone': - $element['services'][$key]['attributes'] = new Attribute(array('g:plusone:size' => ($isvertical ? 'tall' : 'standard'))); + $attributes['g:plusone:size'] = $isvertical ? 'tall' : 'standard'; break; case 'tweet': - $element['services'][$key]['attributes'] = new Attribute(array( - 'tw:count' => ($isvertical ? 'vertical' : 'horizontal'), - 'tw:via' => '' // TODO: D7 used AddThis::getInstance()->getTwitterVia() - )); + $attributes['tw:count'] = $isvertical ? 'vertical' : 'horizontal'; + // $attributes['tw:via'] = $isvertical ? 'vertical' : 'horizontal'; // TODO: D7 used AddThis::getInstance()->getTwitterVia() break; - /* - * case 'bubble_style': - $element['services'][$key]['attributes'] = new Attribute(array( - 'addthis_counter', - 'addthis_bubble_style' - )); + case 'bubble_style': + $attributes['class'][] = 'addthis_counter'; + $attributes['class'][] = 'addthis_bubble_style'; break; case 'pill_style': - $element['services'][$key]['attributes'] = new Attribute(array( - 'addthis_counter', - 'addthis_pill_style' - )); + $attributes['class'][] = 'addthis_counter'; + $attributes['class'][] = 'addthis_pill_style'; break; - */ } + + $element['services'][$key]['attributes'] = new Attribute($attributes); + } diff --git a/templates/addthis-basic-toolbox.html.twig b/templates/addthis-basic-toolbox.html.twig index 13ae9b0..5a5944a 100644 --- a/templates/addthis-basic-toolbox.html.twig +++ b/templates/addthis-basic-toolbox.html.twig @@ -6,10 +6,11 @@ * - element: AddThis Wrapper to be rendered * */ + // class="addthis_button_{{ service.service }}" #}
{% for service in element.services %} - + {% endfor %}
From 3731e7df4c86d9e9dbf07ac115e745adbd31ef67 Mon Sep 17 00:00:00 2001 From: Jason Enter Date: Wed, 26 Aug 2015 14:47:02 -0400 Subject: [PATCH 65/77] first draft of removing constants and old functions --- src/AddThis.php | 138 ++---------------------------------------------- 1 file changed, 4 insertions(+), 134 deletions(-) diff --git a/src/AddThis.php b/src/AddThis.php index 08dbe07..879db13 100644 --- a/src/AddThis.php +++ b/src/AddThis.php @@ -13,36 +13,12 @@ use Drupal\Component\Utility\SafeMarkup; class AddThis { - const BLOCK_NAME = 'addthis_block'; - const DEFAULT_CUSTOM_CONFIGURATION_CODE = 'var addthis_config = {}'; - const DEFAULT_FORMATTER = 'addthis_default_formatter'; - const DEFAULT_NUMBER_OF_PREFERRED_SERVICES = 4; - const FIELD_TYPE = 'addthis'; - const MODULE_NAME = 'addthis'; - const PERMISSION_ADMINISTER_ADDTHIS = 'administer addthis'; - const PERMISSION_ADMINISTER_ADVANCED_ADDTHIS = 'administer advanced addthis'; - const STYLE_KEY = 'addthis_style'; - const WIDGET_TYPE = 'addthis_button_widget'; - - // AddThis attribute and parameter names (as defined in AddThis APIs). - const PROFILE_ID_QUERY_PARAMETER = 'pubid'; - const TITLE_ATTRIBUTE = 'addthis:title'; - const URL_ATTRIBUTE = 'addthis:url'; - // Persistent variable keys. - const BLOCK_WIDGET_TYPE_KEY = 'addthis_block_widget_type'; - const BLOCK_WIDGET_SETTINGS_KEY = 'addthis_block_widget_settings'; - const BOOKMARK_URL_KEY = 'addthis_bookmark_url'; - const CUSTOM_CONFIGURATION_CODE_ENABLED_KEY = 'addthis_custom_configuration_code_enabled'; - const CUSTOM_CONFIGURATION_CODE_KEY = 'addthis_custom_configuration_code'; + /* const SERVICES_CSS_URL_KEY = 'addthis_services_css_url'; - const SERVICES_JSON_URL_KEY = 'addthis_services_json_url'; - - const WIDGET_JS_URL_KEY = 'addthis_widget_js_url'; - const WIDGET_JS_LOAD_DOMREADY = 'addthis_widget_load_domready'; - const WIDGET_JS_LOAD_ASYNC = 'addthis_widget_load_async'; - const WIDGET_JS_INCLUDE = 'addthis_widget_include'; + const SERVICES_JSON_URL_KEY = 'addthis_services_json_url'; + */ // External resources. const DEFAULT_BOOKMARK_URL = 'http://www.addthis.com/bookmark.php?v=300'; @@ -90,7 +66,7 @@ class AddThis { /** * @param \Drupal\addthis\AddThisScriptManager $addThisScriptManager */ - public function __construct(\Drupal\addthis\AddThisScriptManager $addThisScriptManager, \Drupal\Core\Config\ConfigFactory $configFactory){ + public function __construct(\Drupal\addthis\AddThisScriptManager $addThisScriptManager, \Drupal\Core\Config\ConfigFactory $configFactory) { $this->add_this_script_manager = $addThisScriptManager; $this->config_factory = $configFactory; $this->json = new AddThisJson(); @@ -134,30 +110,6 @@ public function getServices() { } - /** - * Get the settings used by the block display. - */ - public function getBlockDisplaySettings($widget_type) { - $block_widget_settings = $this->config->get(self::BLOCK_WIDGET_SETTINGS_KEY); - $block_widget_settings = isset($block_widget_settings) ? $block_widget_settings : NULL; - $settings = $block_widget_settings; - - if ($settings == NULL && $widget_type != self::WIDGET_TYPE_DISABLED) { - $settings = \Drupal::service('plugin.manager.field.formatter') - ->getDefaultSettings($widget_type); - - } - - return $settings; - } - - - public function getServicesCssUrl() { - $services_css_url_key = $this->config->get(self::SERVICES_CSS_URL_KEY); - $services_css_url_key = isset($services_css_url_key) ? $services_css_url_key : self::DEFAULT_SERVICES_CSS_URL; - return check_url($services_css_url_key); - } - public function getServicesJsonUrl() { $service_json_url_key = $this->config->get(self::SERVICES_JSON_URL_KEY); $service_json_url_key = isset($service_json_url_key) ? $service_json_url_key : self::DEFAULT_SERVICES_JSON_URL; @@ -165,86 +117,4 @@ public function getServicesJsonUrl() { } - /** - * Return the type of inclusion. - * - * @return string - * Retuns domready or async. - */ - public function getWidgetJsInclude() { - $widget_js_include = $this->config->get(self::WIDGET_JS_INCLUDE); - $widget_js_include = isset($widget_js_include) ? $widget_js_include : self::DEFAULT_WIDGET_JS_INCLUDE; - return $widget_js_include; - } - - /** - * Return if domready loading should be active. - * - * @return bool - * Returns TRUE if domready is enabled. - */ - public function getWidgetJsDomReady() { - $widget_js_load_domready = $this->config->get(self::WIDGET_JS_LOAD_DOMREADY); - $widget_js_load_domready = isset($widget_js_load_domready) ? $widget_js_load_domready : self::DEFAULT_WIDGET_JS_LOAD_DOMREADY; - return $widget_js_load_domready; - } - - /** - * Return if async initialization should be active. - * - * @return bool - * Returns TRUE if async is enabled. - */ - public function getWidgetJsAsync() { - $widget_js_load_async = $this->config->get(self::WIDGET_JS_LOAD_ASYNC); - $widget_js_load_async = isset($widget_js_load_async) ? $widget_js_load_async : self::DEFAULT_WIDGET_JS_LOAD_ASYNC; - return $widget_js_load_async; - } - - - public function getCustomConfigurationCode() { - $custom_configuration_code = $this->config->get(self::CUSTOM_CONFIGURATION_CODE_KEY); - $custom_configuration_code = isset($custom_configuration_code) ? $custom_configuration_code : self::DEFAULT_CUSTOM_CONFIGURATION_CODE; - return $custom_configuration_code; - } - - public function isCustomConfigurationCodeEnabled() { - $custom_configuration_code_enabled = $this->config->get(self::CUSTOM_CONFIGURATION_CODE_ENABLED_KEY); - $custom_configuration_code_enabled = isset($custom_configuration_code_enabled) ? $custom_configuration_code_enabled : FALSE; - - return (boolean) $custom_configuration_code_enabled; - } - - public function getBaseWidgetJsUrl() { - $widget_js_url = $this->config->get(self::WIDGET_JS_URL_KEY); - $widget_js_url = isset($widget_js_url) ? $widget_js_url : self::DEFAULT_WIDGET_JS_URL; - return check_url($widget_js_url); - } - - public function getBaseBookmarkUrl() { - $bookmark_url = $this->config->get(self::BOOKMARK_URL_KEY); - $bookmark_url = isset($bookmark_url) ? $bookmark_url : self::DEFAULT_BOOKMARK_URL; - return check_url($bookmark_url); - } - - - public function getFullBookmarkUrl() { - return $this->getBaseBookmarkUrl() . $this->getProfileIdQueryParameterPrefixedWithAmp(); - } - - private function getProfileIdQueryParameter($prefix) { - $profileId = $this->getProfileId(); - return !empty($profileId) ? $prefix . self::PROFILE_ID_QUERY_PARAMETER . '=' . $profileId : ''; - } - - private function getProfileIdQueryParameterPrefixedWithAmp() { - return $this->getProfileIdQueryParameter('&'); - } - - private function getProfileIdQueryParameterPrefixedWithHash() { - return $this->getProfileIdQueryParameter('#'); - } - - - } \ No newline at end of file From ca8db7bde8aab709af278d201ff090cb2c74b8c5 Mon Sep 17 00:00:00 2001 From: Jason Enter Date: Wed, 26 Aug 2015 14:55:00 -0400 Subject: [PATCH 66/77] move cleanup based on old elements --- src/AddThis.php | 55 ++++++------------------------------------------- 1 file changed, 6 insertions(+), 49 deletions(-) diff --git a/src/AddThis.php b/src/AddThis.php index 879db13..a4baa01 100644 --- a/src/AddThis.php +++ b/src/AddThis.php @@ -14,55 +14,6 @@ class AddThis { - // Persistent variable keys. - /* - const SERVICES_CSS_URL_KEY = 'addthis_services_css_url'; - const SERVICES_JSON_URL_KEY = 'addthis_services_json_url'; - */ - - // External resources. - const DEFAULT_BOOKMARK_URL = 'http://www.addthis.com/bookmark.php?v=300'; - const DEFAULT_SERVICES_CSS_URL = 'http://cache.addthiscdn.com/icons/v1/sprites/services.css'; - const DEFAULT_SERVICES_JSON_URL = 'http://cache.addthiscdn.com/services/v1/sharing.en.json'; - const DEFAULT_WIDGET_JS_URL = 'http://s7.addthis.com/js/300/addthis_widget.js'; - const DEFAULT_WIDGET_JS_LOAD_DOMREADY = TRUE; - const DEFAULT_WIDGET_JS_LOAD_ASYNC = FALSE; - - // Type of inclusion. - // 0 = don't include, 1 = pages no admin, 2 = on usages only. - const DEFAULT_WIDGET_JS_INCLUDE = 2; - const WIDGET_JS_INCLUDE_NONE = 0; - const WIDGET_JS_INCLUDE_PAGE = 1; - const WIDGET_JS_INCLUDE_USAGE = 2; - - - // Internal resources. - const ADMIN_CSS_FILE = 'addthis.admin.css'; - const ADMIN_INCLUDE_FILE = 'includes/addthis.admin.inc'; - - // Widget types. - const WIDGET_TYPE_DISABLED = 'addthis_disabled'; - - // Styles. - const CSS_32x32 = 'addthis_32x32_style'; - const CSS_16x16 = 'addthis_16x16_style'; - private static $instance; - - /* @var AddThisJson */ - private $json; - - private $config; - - /** - * @var \Drupal\addthis\AddThisScriptManager. - */ - protected $add_this_script_manager; - - /** - * @var \Drupal\Core\Config\ConfigFactory - */ - protected $config_factory; - /** * @param \Drupal\addthis\AddThisScriptManager $addThisScriptManager */ @@ -73,6 +24,11 @@ public function __construct(\Drupal\addthis\AddThisScriptManager $addThisScriptM $this->setConfig(); } + + /* @var AddThisJson */ + private $json; + private $config; + /** * Set the json object. */ @@ -109,6 +65,7 @@ public function getServices() { return $rows; } + const DEFAULT_SERVICES_JSON_URL = 'http://cache.addthiscdn.com/services/v1/sharing.en.json'; public function getServicesJsonUrl() { $service_json_url_key = $this->config->get(self::SERVICES_JSON_URL_KEY); From 562091e08154b7ca90a9a1419e2c7ee5f0c6a7c7 Mon Sep 17 00:00:00 2001 From: Jason Enter Date: Wed, 26 Aug 2015 15:34:35 -0400 Subject: [PATCH 67/77] adding required const --- src/AddThis.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/AddThis.php b/src/AddThis.php index a4baa01..7384f59 100644 --- a/src/AddThis.php +++ b/src/AddThis.php @@ -14,6 +14,9 @@ class AddThis { + const SERVICES_CSS_URL_KEY = 'addthis_services_css_url'; + const SERVICES_JSON_URL_KEY = 'addthis_services_json_url'; + /** * @param \Drupal\addthis\AddThisScriptManager $addThisScriptManager */ From 61d7013c928159db5f4b200bad80b61791d8569b Mon Sep 17 00:00:00 2001 From: Jason Enter Date: Wed, 26 Aug 2015 15:54:41 -0400 Subject: [PATCH 68/77] quick adjustments --- src/AddThis.php | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/AddThis.php b/src/AddThis.php index 7384f59..32c101d 100644 --- a/src/AddThis.php +++ b/src/AddThis.php @@ -17,7 +17,11 @@ class AddThis { const SERVICES_CSS_URL_KEY = 'addthis_services_css_url'; const SERVICES_JSON_URL_KEY = 'addthis_services_json_url'; - /** + /* @var AddThisJson */ + private $json; + private $config; + + /** * @param \Drupal\addthis\AddThisScriptManager $addThisScriptManager */ public function __construct(\Drupal\addthis\AddThisScriptManager $addThisScriptManager, \Drupal\Core\Config\ConfigFactory $configFactory) { @@ -27,11 +31,6 @@ public function __construct(\Drupal\addthis\AddThisScriptManager $addThisScriptM $this->setConfig(); } - - /* @var AddThisJson */ - private $json; - private $config; - /** * Set the json object. */ @@ -43,7 +42,6 @@ public function setConfig() { $this->config = $this->config_factory->get('addthis.settings'); } - public function getServices() { $rows = array(); $services = $this->json->decode($this->getServicesJsonUrl()); @@ -61,18 +59,15 @@ public function getServices() { '#type' => 'inline_template', '#template' => ' ' . $serviceName, ); - //$rows[$serviceCode] = ' ' . $serviceName; $rows[$serviceCode] = $service; } } return $rows; } - const DEFAULT_SERVICES_JSON_URL = 'http://cache.addthiscdn.com/services/v1/sharing.en.json'; - public function getServicesJsonUrl() { $service_json_url_key = $this->config->get(self::SERVICES_JSON_URL_KEY); - $service_json_url_key = isset($service_json_url_key) ? $service_json_url_key : self::DEFAULT_SERVICES_JSON_URL; + $service_json_url_key = isset($service_json_url_key) ? $service_json_url_key : 'http://cache.addthiscdn.com/services/v1/sharing.en.json'; return check_url($service_json_url_key); } From 3192e1469a9b874b2a010eb98787c3020d490369 Mon Sep 17 00:00:00 2001 From: Jason Enter Date: Wed, 26 Aug 2015 16:56:46 -0400 Subject: [PATCH 69/77] moving constants --- src/AddThis.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/AddThis.php b/src/AddThis.php index 32c101d..d2d6b90 100644 --- a/src/AddThis.php +++ b/src/AddThis.php @@ -14,14 +14,11 @@ class AddThis { - const SERVICES_CSS_URL_KEY = 'addthis_services_css_url'; - const SERVICES_JSON_URL_KEY = 'addthis_services_json_url'; - /* @var AddThisJson */ private $json; private $config; - /** + /** * @param \Drupal\addthis\AddThisScriptManager $addThisScriptManager */ public function __construct(\Drupal\addthis\AddThisScriptManager $addThisScriptManager, \Drupal\Core\Config\ConfigFactory $configFactory) { @@ -66,10 +63,10 @@ public function getServices() { } public function getServicesJsonUrl() { - $service_json_url_key = $this->config->get(self::SERVICES_JSON_URL_KEY); + $service_json_url_key = $this->config->get('addthis_services_json_url'); $service_json_url_key = isset($service_json_url_key) ? $service_json_url_key : 'http://cache.addthiscdn.com/services/v1/sharing.en.json'; return check_url($service_json_url_key); } -} \ No newline at end of file +} From ad325f55368784edd8d92d776d4840868f5306f2 Mon Sep 17 00:00:00 2001 From: John Doyle Date: Thu, 27 Aug 2015 16:14:39 -0400 Subject: [PATCH 70/77] Cleaned up code. Fixed some bugs found during testing. Implemented unit tests for Block, Permissions and Fields --- addthis.module | 6 - config/schema/addthis.schema.yml | 75 +++++-- src/AddThis.php | 3 +- src/AddThisBasicToolboxFormTrait.php | 2 +- src/Element/AddThisBasicButton.php | 5 +- src/Element/AddThisBasicToolbox.php | 3 +- src/Form/AddThisSettingsForm.php | 71 ++++--- src/Plugin/Block/AddThisBlock.php | 56 +++--- src/Plugin/Field/FieldType/AddThisItem.php | 41 +--- src/Tests/AddThisBlockTest.php | 216 +++++++++++++++++++++ src/Tests/AddThisFieldTest.php | 124 ++++++++++++ src/Tests/AddThisFieldWebTestBase.php | 151 ++++++++++++++ src/Tests/AddThisPermissionsTest.php | 63 ++++++ 13 files changed, 677 insertions(+), 139 deletions(-) create mode 100644 src/Tests/AddThisBlockTest.php create mode 100644 src/Tests/AddThisFieldTest.php create mode 100644 src/Tests/AddThisFieldWebTestBase.php create mode 100644 src/Tests/AddThisPermissionsTest.php diff --git a/addthis.module b/addthis.module index f1895a0..02c123e 100644 --- a/addthis.module +++ b/addthis.module @@ -5,12 +5,6 @@ */ function addthis_theme() { return [ - 'addthis_wrapper' => [ - 'render element' => 'element', - ], - 'addthis_element' => [ - 'render element' => 'element', - ], 'addthis_basic_button' => [ 'render element' => 'element', ], diff --git a/config/schema/addthis.schema.yml b/config/schema/addthis.schema.yml index dbeba51..ca47f7b 100644 --- a/config/schema/addthis.schema.yml +++ b/config/schema/addthis.schema.yml @@ -12,24 +12,24 @@ addthis.settings: type: mapping label: 'Style' mapping: - addthis_co_brand: - type: string - label: 'Branding text' - addthis_ui_header_color: - type: string - label: 'Header text color' - addthis_ui_header_background_color: - type: string - label: 'Header background color' - addthis_click_to_open_compact_menu_enabled: - type: boolean - label: 'Open compact menu on click' - addthis_open_windows_enabled: - type: boolean - label: 'Use pop-up windows' - addthis_ui_delay: - type: string - label: 'Menu open delay' + addthis_co_brand: + type: string + label: 'Branding text' + addthis_ui_header_color: + type: string + label: 'Header text color' + addthis_ui_header_background_color: + type: string + label: 'Header background color' + addthis_click_to_open_compact_menu_enabled: + type: integer + label: 'Open compact menu on click' + addthis_open_windows_enabled: + type: integer + label: 'Use pop-up windows' + addthis_ui_delay: + type: string + label: 'Menu open delay' enabled_services: type: mapping label: 'Compact menu enabled services' @@ -122,4 +122,41 @@ addthis.settings.advanced: label: 'Initialize asynchronously through addthis.init()' addthis_widget_include: type: integer - label: 'Load widget js.' \ No newline at end of file + label: 'Load widget js.' +block.settings.addthis_block: + type: block_settings + label: 'AddThis Block Configuration' + mapping: + display_type: + type: string + label: 'Display Type' + basic_button: + type: mapping + label: 'Basic Button Block Settings' + mapping: + button_size: + type: string + label: 'Button Size' + extra_css: + type: string + label: 'Extra CSS' + basic_toolbox: + type: mapping + label: 'Basic Toolbox Block Settings' + mapping: + share_services: + type: string + label: 'Share Services' + buttons_size: + type: string + label: 'Button Size' + counter_orientation: + type: string + label: 'Counter Orientation' + extra_css: + type: string + label: 'Extra CSS' + + + + diff --git a/src/AddThis.php b/src/AddThis.php index 08dbe07..6977fbc 100644 --- a/src/AddThis.php +++ b/src/AddThis.php @@ -126,8 +126,7 @@ public function getServices() { '#type' => 'inline_template', '#template' => ' ' . $serviceName, ); - //$rows[$serviceCode] = ' ' . $serviceName; - $rows[$serviceCode] = $service; + $rows[$serviceCode] = render($service); } } return $rows; diff --git a/src/AddThisBasicToolboxFormTrait.php b/src/AddThisBasicToolboxFormTrait.php index 4b47190..15a6c23 100644 --- a/src/AddThisBasicToolboxFormTrait.php +++ b/src/AddThisBasicToolboxFormTrait.php @@ -107,7 +107,7 @@ public static function addThisDisplayElementServicesValidate($element, $form_sta // Return error. if ($bad) { - $form_state->setErrorByName($element['name'], t('The declared services are incorrect or nonexistent.')); + $form_state->setErrorByName($element['#title'], t('The declared services are incorrect or nonexistent.')); } } diff --git a/src/Element/AddThisBasicButton.php b/src/Element/AddThisBasicButton.php index c15c075..9f7c5ec 100644 --- a/src/Element/AddThisBasicButton.php +++ b/src/Element/AddThisBasicButton.php @@ -21,7 +21,6 @@ class AddThisBasicButton extends RenderElement { */ public function getInfo() { $class = get_class($this); - $config = $this->configuration; return [ '#theme' => 'addthis_basic_button', '#size' => 'addthis_16x16_style', @@ -32,10 +31,10 @@ public function getInfo() { ]; } - public function preRender($element) { + public static function preRender($element) { // Add button. $button_img = 'http://s7.addthis.com/static/btn/sm-share-en.gif'; - if ($element['size'] === 'big') { + if ($element['#size'] === 'big') { $button_img = 'http://s7.addthis.com/static/btn/v2/lg-share-en.gif'; } diff --git a/src/Element/AddThisBasicToolbox.php b/src/Element/AddThisBasicToolbox.php index e33ce74..6e499a0 100644 --- a/src/Element/AddThisBasicToolbox.php +++ b/src/Element/AddThisBasicToolbox.php @@ -21,7 +21,6 @@ class AddThisBasicToolbox extends RenderElement { */ public function getInfo() { $class = get_class($this); - $config = $this->configuration; return [ '#theme' => 'addthis_basic_toolbox', '#size' => 'addthis_16x16_style', @@ -35,7 +34,7 @@ public function getInfo() { } - public function preRender($element) { + public static function preRender($element) { // Add Script. $script_manager = \Drupal::getContainer()->get('addthis.script_manager'); $script_manager->attachJsToElement($element); diff --git a/src/Form/AddThisSettingsForm.php b/src/Form/AddThisSettingsForm.php index 6aadad4..3fa8ac4 100644 --- a/src/Form/AddThisSettingsForm.php +++ b/src/Form/AddThisSettingsForm.php @@ -8,10 +8,7 @@ use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Extension\ModuleHandler; use Drupal\Component\Utility\Xss; -use Drupal\addthis\AddThis; -use Symfony\Component\Validator\Constraints\False; /** * Defines a form to configure maintenance settings for this site. @@ -58,19 +55,19 @@ public function buildForm(array $form, FormStateInterface $form_state) { $form['#attached']['library'][] = 'addthis/addthis.admin'; // Visual settings. - $form['details_compact_menu'] = array( + $form['compact_menu'] = array( '#type' => 'details', '#title' => t('Compact menu'), '#open' => TRUE, '#description' => '

' . t('Configure the global behavior and style of the compact menu and some additional settings related to the interface.') . '

', '#description_display' => 'before' ); - $form['details_compact_menu']['details_menu_style'] = array( + $form['compact_menu']['menu_style'] = array( '#type' => 'details', '#title' => t('Style'), '#open' => FALSE, ); - $form['details_compact_menu']['details_menu_style']['addthis_co_brand'] = array( + $form['compact_menu']['menu_style']['addthis_co_brand'] = array( '#type' => 'textfield', '#title' => t('Branding text'), '#description' => t('Additional branding message to be rendered in the upper-right-hand corner of the compact menus.
Should be less than 15 characters in most cases to render properly.'), @@ -78,7 +75,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#required' => FALSE, '#maxlength' => 15, ); - $form['details_compact_menu']['details_menu_style']['addthis_ui_header_color'] = array( + $form['compact_menu']['menu_style']['addthis_ui_header_color'] = array( '#type' => 'textfield', '#title' => t('Header text color'), '#default_value' => $config->get('compact_menu.menu_style.addthis_ui_header_color'), @@ -87,7 +84,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#maxlength' => 7, '#required' => FALSE, ); - $form['details_compact_menu']['details_menu_style']['addthis_ui_header_background_color'] = array( + $form['compact_menu']['menu_style']['addthis_ui_header_background_color'] = array( '#type' => 'textfield', '#title' => t('Header background color'), '#default_value' => $config->get('compact_menu.menu_style.addthis_ui_header_background_color'), @@ -96,21 +93,21 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#maxlength' => 7, '#required' => FALSE, ); - $form['details_compact_menu']['details_menu_style']['addthis_click_to_open_compact_menu_enabled'] = array( + $form['compact_menu']['menu_style']['addthis_click_to_open_compact_menu_enabled'] = array( '#type' => 'checkbox', '#title' => t('Open compact menu on click'), '#description' => t('Default behavior is to open compact menu on hover.'), '#default_value' => $config->get('compact_menu.menu_style.addthis_click_to_open_compact_menu_enabled'), '#required' => FALSE, ); - $form['details_compact_menu']['details_menu_style']['addthis_open_windows_enabled'] = array( + $form['compact_menu']['menu_style']['addthis_open_windows_enabled'] = array( '#type' => 'checkbox', '#title' => t('Use pop-up windows'), '#description' => t('If checked, all shares will open in a new pop-up window instead of a new tab or regular browser window.'), '#default_value' => $config->get('compact_menu.menu_style.addthis_open_windows_enabled'), '#required' => FALSE, ); - $form['details_compact_menu']['details_menu_style']['addthis_ui_delay'] = array( + $form['compact_menu']['menu_style']['addthis_ui_delay'] = array( '#type' => 'textfield', '#title' => t('Menu open delay'), '#description' => t('Delay, in milliseconds, before compact menu appears when mousing over a regular button. Capped at 500 ms.'), @@ -121,7 +118,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { ); // Enabled services settings. - $form['details_compact_menu']['enabled_services_details'] = array( + $form['compact_menu']['enabled_services'] = array( '#type' => 'details', '#title' => t('Compact menu enabled services'), '#description' => t('The sharing services you select here will be displayed in the compact menu. If you select no services, AddThis will provide a list of frequently used services. This list is updated regularly. Notice that this setting does not define what services should be display in a toolbox.'), @@ -131,7 +128,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { $add_this_service = \Drupal::service('addthis.addthis'); - $form['details_compact_menu']['enabled_services_details']['addthis_enabled_services'] = array( + $form['compact_menu']['enabled_services']['addthis_enabled_services'] = array( '#type' => 'checkboxes', '#title' => t('Enabled services'), '#options' => $add_this_service->getServices(), @@ -141,26 +138,26 @@ public function buildForm(array $form, FormStateInterface $form_state) { ); // Additional visual settings. - $form['details_compact_menu']['details_additionals'] = array( + $form['compact_menu']['additionals'] = array( '#type' => 'details', '#title' => t('Additional configuration'), '#open' => FALSE, ); - $form['details_compact_menu']['details_additionals']['addthis_standard_css_enabled'] = array( + $form['compact_menu']['additionals']['addthis_standard_css_enabled'] = array( '#type' => 'checkbox', '#title' => t('Use standard AddThis stylesheet'), '#description' => t('If not checked, AddThis will not load standard CSS file, allowing you to style everything yourself without incurring the cost of an additional load.'), '#default_value' => $config->get('compact_menu.additionals.addthis_standard_css_enabled'), '#required' => FALSE, ); - $form['details_compact_menu']['details_additionals']['addthis_508_compliant'] = array( + $form['compact_menu']['additionals']['addthis_508_compliant'] = array( '#type' => 'checkbox', '#title' => t('508 compliant'), '#description' => 'If checked, clicking the AddThis button will open a new window to a page that is keyboard navigable.', '#default_value' => $config->get('compact_menu.additionals.addthis_508_compliant'), '#required' => FALSE, ); - $form['details_compact_menu']['details_additionals']['addthis_addressbook_enabled'] = array( + $form['compact_menu']['additionals']['addthis_addressbook_enabled'] = array( '#type' => 'checkbox', '#title' => t('Use addressbook'), '#description' => 'If checked, the user will be able import their contacts from popular webmail services when using AddThis\'s email sharing.', @@ -169,14 +166,14 @@ public function buildForm(array $form, FormStateInterface $form_state) { ); // Excluded Services. - $form['details_excluded_services'] = array( + $form['excluded_services'] = array( '#type' => 'details', '#title' => t('Excluded services'), '#description' => t('The sharing services you select here will be excluded from all AddThis menus. This applies globally.'), '#description_display' => 'before', '#open' => FALSE, ); - $form['details_excluded_services']['addthis_excluded_services'] = array( + $form['excluded_services']['addthis_excluded_services'] = array( '#type' => 'checkboxes', '#title' => t('Excluded services'), '#options' => $add_this_service->getServices(), @@ -188,14 +185,14 @@ public function buildForm(array $form, FormStateInterface $form_state) { //Analytics settings. $profile_id = $config->get('analytics.addthis_profile_id'); $can_track_clicks = empty($profile_id) ? FALSE : TRUE; - $form['details_analytics'] = array( + $form['analytics'] = array( '#type' => 'details', '#title' => t('Analytics and Tracking'), '#open' => $can_track_clicks ? TRUE : FALSE, ); if (!$can_track_clicks) { - $form['details_analytics']['can_track_notice'] = array( + $form['analytics']['can_track_notice'] = array( '#theme' => 'html_tag', '#tag' => 'div', '#value' => t('For click analytics and statistics you have to provide a ProfileID from AddThis.com. Register here.'), @@ -203,7 +200,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { ); } - $form['details_analytics']['addthis_profile_id'] = array( + $form['analytics']['addthis_profile_id'] = array( '#type' => 'textfield', '#title' => t('AddThis ProfileID'), '#default_value' => $config->get('analytics.addthis_profile_id'), @@ -211,7 +208,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#size' => 25, '#description' => t('ProfileID at AddThis.com. Required for statistics.'), ); - $form['details_analytics']['addthis_clickback_tracking_enabled'] = array( + $form['analytics']['addthis_clickback_tracking_enabled'] = array( '#type' => 'checkbox', '#title' => t('Track clickback'), '#description' => 'Check to allow AddThis to append a variable to your URLs upon sharing. AddThis will use this to track how many people come back to your content via links shared with AddThis. Highly recommended. Always global.', @@ -229,19 +226,19 @@ public function buildForm(array $form, FormStateInterface $form_state) { else { $rdf_description = t('Check to enable Facebook Like tracking support. Always global.'); } - $form['details_analytics']['title_facebook'] = array( + $form['analytics']['title_facebook'] = array( '#theme' => 'html_tag', '#tag' => 'div', '#value' => '' . t('Facebook') . '', ); - $form['details_analytics']['facebook_notice'] = array( + $form['analytics']['facebook_notice'] = array( '#theme' => 'html_tag', '#tag' => 'p', '#value' => $rdf_description, '#access' => !$rdf_enabled, ); - $form['details_analytics']['addthis_facebook_like_count_support_enabled'] = array( + $form['analytics']['addthis_facebook_like_count_support_enabled'] = array( '#type' => 'checkbox', '#title' => t('Enable Facebook Like tracking'), '#description' => Xss::filter($rdf_description, array('span')), @@ -255,16 +252,16 @@ public function buildForm(array $form, FormStateInterface $form_state) { ->moduleExists('google_analytics'); //@TODO Get back to this. $google_analytics_config = $this->config_factory->get('google_analytics.settings'); - $google_analytics_account = $google_analytics_config->get('account'); + $google_analytics_account = $google_analytics_config->get('google_analytics_account'); $is_google_analytics_setup = $can_do_google_social_tracking && isset($google_analytics_account); - $form['details_analytics']['google_analytics'] = array( + $form['analytics']['google_analytics'] = array( '#type' => 'markup', '#prefix' => '
', '#suffix' => '
', '#markup' => '' . t('Google Analytics') . '', ); if (!$can_do_google_social_tracking) { - $form['details_analytics']['can_do_google_analytics'] = array( + $form['analytics']['can_do_google_analytics'] = array( '#type' => 'markup', '#prefix' => '

', '#suffix' => '

', @@ -272,14 +269,14 @@ public function buildForm(array $form, FormStateInterface $form_state) { ); } elseif ($can_do_google_social_tracking && !$is_google_analytics_setup) { - $form['details_analytics']['can_do_google_analytics'] = array( + $form['analytics']['can_do_google_analytics'] = array( '#type' => 'markup', '#prefix' => '

', '#suffix' => '

', - '#markup' => '' . t('Configure the Google Analytics module correctly with the account code to use this feature.') . '', + '#value' => '' . t('Configure the Google Analytics module correctly with the account code to use this feature.') . '', ); } - $form['details_analytics']['addthis_google_analytics_tracking_enabled'] = array( + $form['analytics']['addthis_google_analytics_tracking_enabled'] = array( '#type' => 'checkbox', '#title' => t('Track with Google Analytics'), '#description' => t('Check to track shares in your Google Analytics account reports (more info). Always global.'), @@ -287,7 +284,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#required' => FALSE, '#disabled' => !$is_google_analytics_setup, ); - $form['details_analytics']['addthis_google_analytics_social_tracking_enabled'] = array( + $form['analytics']['addthis_google_analytics_social_tracking_enabled'] = array( '#type' => 'checkbox', '#title' => t('Track with Google Analytics social'), '#description' => t('Check to track shares in the new Google Analytics social interaction reports (more info). Always global.'), @@ -297,25 +294,25 @@ public function buildForm(array $form, FormStateInterface $form_state) { ); // Third party settings. - $form['third_party_details'] = array( + $form['third_party'] = array( '#type' => 'details', '#title' => t('Third party settings'), '#open' => FALSE, ); - $form['third_party_details']['twitter_service'] = array( + $form['third_party']['twitter_service'] = array( '#type' => 'fieldset', '#title' => t('Twitter'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); - $form['third_party_details']['twitter_service']['addthis_twitter_via'] = array( + $form['third_party']['twitter_service']['addthis_twitter_via'] = array( '#type' => 'textfield', '#title' => t('Send via'), '#description' => t('When sending a tweet this is the screen name of the user to attribute the Tweet to. (Relates to Tweet Button)'), '#default_value' => $config->get('third_party.addthis_twitter_via'), '#size' => 15, ); - $form['third_party_details']['twitter_service']['addthis_twitter_template'] = array( + $form['third_party']['twitter_service']['addthis_twitter_template'] = array( '#type' => 'textfield', '#title' => t('Template text'), '#description' => t('The {{title}} and {{url}} are replaced from the Twitter Button.'), diff --git a/src/Plugin/Block/AddThisBlock.php b/src/Plugin/Block/AddThisBlock.php index ae770a0..332e023 100644 --- a/src/Plugin/Block/AddThisBlock.php +++ b/src/Plugin/Block/AddThisBlock.php @@ -30,7 +30,7 @@ class AddThisBlock extends BlockBase { */ public function defaultConfiguration() { return array( - 'type' => 'addthis_disabled', + 'display_type' => 'addthis_basic_button', 'basic_toolbox' => $this->addThisBasicToolboxGetDefaults(), 'basic_button' => $this->addThisBasicButtonGetDefaults(), ); @@ -42,29 +42,25 @@ public function defaultConfiguration() { function blockForm($form, FormStateInterface $form_state) { $settings = $this->getConfiguration(); - $type = $settings['type']; - $rebuild = $form_state->getValue([ - 'settings', + //get type from config + $default_type = $settings['display_type']; + + $selected_type = $form_state->getValue([ 'settings', 'addthis_settings', - 'type' + 'display_type' ]); - if (isset($rebuild)) { - $type = $form_state->getValue([ - 'settings', - 'settings', - 'addthis_settings', - 'type' - ]); - } - $form['settings']['addthis_settings'] = array( + $selected_type = isset($selected_type) ? $selected_type : $default_type; + + + $form['addthis_settings'] = array( '#type' => 'fieldset', '#title' => 'Display settings', ); - $form['settings']['addthis_settings']['type'] = array( + $form['addthis_settings']['display_type'] = array( '#type' => 'select', '#title' => t('Formatter for @title', array('@title' => 'AddThis block')), '#title_display' => 'invisible', @@ -72,25 +68,25 @@ function blockForm($form, FormStateInterface $form_state) { 'addthis_basic_button' => 'AddThis Basic Button', 'addthis_basic_toolbox' => 'AddThis Basic Toolbox', ], - '#default_value' => $settings['type'], + '#default_value' => isset($default_type) ? $default_type : 'addthis_basic_button', '#attributes' => array('class' => array('addthis-display-type')), '#ajax' => array( 'callback' => array($this, 'addthisAjaxCallback'), 'wrapper' => 'addthis_type_settings', ), ); - $form['settings']['addthis_settings']['type_settings'] = array( + $form['addthis_settings']['type_settings'] = array( '#prefix' => '
'
', ); - if ($type == 'addthis_basic_toolbox') { + if (isset($selected_type) && $selected_type == 'addthis_basic_toolbox') { $basicToolbox = $this->addThisBasicToolboxForm($settings['basic_toolbox']); - $form['settings']['addthis_settings']['type_settings']['basic_toolbox'] = $basicToolbox; + $form['addthis_settings']['type_settings']['basic_toolbox'] = $basicToolbox; } else { - if ($type == 'addthis_basic_button') { - $basicButton = $this->addThisBasicButtonForm($settings['basic_button']); - $form['settings']['addthis_settings']['type_settings']['basic_button'] = $basicButton; + if (isset($selected_type) && $selected_type == 'addthis_basic_button') { + $basicButton = $this->addThisBasicButtonForm($settings['basic_button']); + $form['addthis_settings']['type_settings']['basic_button'] = $basicButton; } } @@ -105,7 +101,7 @@ function blockForm($form, FormStateInterface $form_state) { * @return mixed */ public function addthisAjaxCallback(array $form, FormStateInterface $form_state) { - return $form['settings']['settings']['addthis_settings']['type_settings']; + return $form['settings']['addthis_settings']['type_settings']; } /** @@ -115,17 +111,15 @@ public function blockSubmit($form, FormStateInterface $form_state) { //@TODO Settings for unselected type get wiped because they dont exist in form. // Try not to overwrite them if possible. - $this->configuration['type'] = $form_state->getValue([ - 'settings', + $this->configuration['display_type'] = $form_state->getValue([ 'addthis_settings', - 'type' + 'display_type' ]); //Handle saving the partial elements provided by AddThisBasicToolboxFormTrait. $basicToolboxKeys = $this->addThisBasicToolboxGetDefaults(); - foreach($basicToolboxKeys as $key => $value) { + foreach ($basicToolboxKeys as $key => $value) { $this->configuration['basic_toolbox'][$key] = $form_state->getValue([ - 'settings', 'addthis_settings', 'type_settings', 'basic_toolbox', @@ -135,9 +129,8 @@ public function blockSubmit($form, FormStateInterface $form_state) { //Handle saving the partial elements provided by AddThisBasicButtonFormTrait. $basicButtonKeys = $this->addThisBasicButtonGetDefaults(); - foreach($basicButtonKeys as $key => $value) { + foreach ($basicButtonKeys as $key => $value) { $this->configuration['basic_button'][$key] = $form_state->getValue([ - 'settings', 'addthis_settings', 'type_settings', 'basic_button', @@ -146,7 +139,6 @@ public function blockSubmit($form, FormStateInterface $form_state) { } - } /** @@ -155,7 +147,7 @@ public function blockSubmit($form, FormStateInterface $form_state) { public function build() { $config = $this->configuration; - switch ($config['type']) { + switch ($config['display_type']) { case 'addthis_basic_button': return [ '#type' => 'addthis_basic_button', diff --git a/src/Plugin/Field/FieldType/AddThisItem.php b/src/Plugin/Field/FieldType/AddThisItem.php index 5d9d06b..cc66b76 100644 --- a/src/Plugin/Field/FieldType/AddThisItem.php +++ b/src/Plugin/Field/FieldType/AddThisItem.php @@ -31,10 +31,7 @@ class AddThisItem extends FieldItemBase { * {@inheritdoc} */ public static function defaultStorageSettings() { - return array( - 'max_length' => 255, - 'is_ascii' => FALSE, - ) + parent::defaultStorageSettings(); + return [] + parent::defaultStorageSettings(); } /** @@ -44,9 +41,8 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) return array( 'columns' => array( 'value' => array( - 'type' => $field_definition->getSetting('is_ascii') === TRUE ? 'varchar_ascii' : 'varchar', - 'length' => (int) $field_definition->getSetting('max_length'), - 'binary' => $field_definition->getSetting('case_sensitive'), + 'type' => 'varchar', + 'length' => 255, ), ), ); @@ -59,32 +55,6 @@ public function isEmpty() { return FALSE; } - /** - * {@inheritdoc} - */ - public function getConstraints() { - $constraints = parent::getConstraints(); - - if ($max_length = $this->getSetting('max_length')) { - $constraint_manager = \Drupal::typedDataManager() - ->getValidationConstraintManager(); - $constraints[] = $constraint_manager->create('ComplexData', array( - 'value' => array( - 'Length' => array( - 'max' => $max_length, - 'maxMessage' => t('%name: may not be longer than @max characters.', array( - '%name' => $this->getFieldDefinition() - ->getLabel(), - '@max' => $max_length - )), - ), - ), - )); - } - - return $constraints; - } - /** * {@inheritdoc} @@ -93,10 +63,7 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel // This is called very early by the user entity roles field. Prevent // early t() calls by using the TranslationWrapper. $properties['value'] = DataDefinition::create('string') - ->setLabel(new TranslationWrapper('Text value')) - ->setSetting('case_sensitive', $field_definition->getSetting('case_sensitive')) - ->setRequired(TRUE); - + ->setLabel(new TranslationWrapper('Text value')); return $properties; } diff --git a/src/Tests/AddThisBlockTest.php b/src/Tests/AddThisBlockTest.php new file mode 100644 index 0000000..4e224b7 --- /dev/null +++ b/src/Tests/AddThisBlockTest.php @@ -0,0 +1,216 @@ +adminUser = $this->drupalCreateUser(array_keys(\Drupal::service('user.permissions')->getPermissions())); + $this->drupalLogin($this->adminUser); + } + /** + * Tests ability to add a block to the page. + * - Adds AddThis block to content region + * - Verifies the block displays + */ + public function testAddThisAddBlockToPage(){ + + $block = $this->drupalPlaceBlock('addthis', [ + 'label' => 'AddThis Block', + 'region' => 'content', + ]); + + $this->drupalGet(''); + $this->assertBlockAppears($block); + } + + /** + * Tests ability to create a block with "addthis_basic_toolbox" and verifies the form fields. + * - Creates an AddThis block + * - Sets Display type to addthis_basic_toolbox + * - Confirms fields are available + * - Saves block settings + * - Confirms configuration was saved successfully. + * + * @throws \Exception + */ + public function testAddThisBlockToolboxConfig(){ + $block = array(); + $block['id'] = 'addthis_block'; + $block['settings[label]'] = $this->randomMachineName(8); + $block['theme'] = $this->config('system.theme')->get('default'); + $block['region'] = 'content'; + $edit = array( + 'settings[label]' => $block['settings[label]'], + 'id' => $block['id'], + 'region' => $block['region'], + 'settings[addthis_settings][display_type]' => 'addthis_basic_toolbox', + ); + + //Create Block with type=addthis_basic_toolbox + $this->drupalPostForm('admin/structure/block/add/' . $block['id'] . '/' . $block['theme'], $edit, t('Save block')); + $this->assertText(t('The block configuration has been saved.'), 'AddThis block has been created.'); + + //Load up new block. + $this->drupalGet('admin/structure/block/manage/' . $block['id']); + //Verify form fields for toolbox are here. + $this->assertFieldByName('settings[addthis_settings][type_settings][basic_toolbox][share_services]'); + $this->assertFieldByName('settings[addthis_settings][type_settings][basic_toolbox][buttons_size]'); + $this->assertFieldByName('settings[addthis_settings][type_settings][basic_toolbox][counter_orientation]'); + $this->assertFieldByName('settings[addthis_settings][type_settings][basic_toolbox][extra_css]'); + + //Test saving and loading config works. + $edit = [ + 'settings[addthis_settings][display_type]' => 'addthis_basic_toolbox', + 'settings[addthis_settings][type_settings][basic_toolbox][share_services]' => 'twitter,facebook,compact', + 'settings[addthis_settings][type_settings][basic_toolbox][buttons_size]' => 'addthis_32x32_style', + 'settings[addthis_settings][type_settings][basic_toolbox][counter_orientation]' => 'vertical', + 'settings[addthis_settings][type_settings][basic_toolbox][extra_css]' => $this->randomMachineName(), + ]; + $this->drupalPostForm('admin/structure/block/manage/' . $block['id'], $edit, t('Save block')); + $this->assertRaw($edit['settings[addthis_settings][display_type]'], 'Content of configurable type successfully verified.'); + $this->assertRaw($edit['settings[addthis_settings][type_settings][basic_toolbox][share_services]'], 'Content of configurable share services block successfully verified.'); + $this->assertRaw($edit['settings[addthis_settings][type_settings][basic_toolbox][buttons_size]'], 'Content of configurable button size block successfully verified.'); + $this->assertRaw($edit['settings[addthis_settings][type_settings][basic_toolbox][counter_orientation]'], 'Content of configurable counter orientation block successfully verified.'); + $this->assertRaw($edit['settings[addthis_settings][type_settings][basic_toolbox][extra_css]'], 'Content of configurable extra css block successfully verified.'); + + + + } + + /** + * Tests the validation for the AddThisBasicToolbox service settings. + * - Creates AddThis block + * - Sets Display type to addthis_basic_toolbox + * - Tests for failed validation for services field + * - Tests for successful validation for services field + * + * @throws \Exception + */ + public function testAddThisBlockToolboxServicesValidation(){ + $block = array(); + $block['id'] = 'addthis_block'; + $block['settings[label]'] = $this->randomMachineName(8); + $block['theme'] = $this->config('system.theme')->get('default'); + $block['region'] = 'content'; + $edit = array( + 'settings[label]' => $block['settings[label]'], + 'id' => $block['id'], + 'region' => $block['region'], + 'settings[addthis_settings][display_type]' => 'addthis_basic_toolbox', + ); + + $this->drupalGet('admin/structure/block/list/' . $this->config('system.theme')->get('default')); + + $this->drupalPostForm('admin/structure/block/add/' . $block['id'] . '/' . $block['theme'], $edit, t('Save block')); + $this->assertText(t('The block configuration has been saved.'), 'AddThis block has been created.'); + + //Test - validation should fail. + $edit = [ + 'settings[addthis_settings][display_type]' => 'addthis_basic_toolbox', + 'settings[addthis_settings][type_settings][basic_toolbox][share_services]' => '', + ]; + $this->drupalPostForm('admin/structure/block/manage/' . $block['id'], $edit, t('Save block')); + $this->assertText(t('Services field is required.'), 'AddThis block validation failed because services was empty.'); + + //Test - validation should fail. + $edit = [ + 'settings[addthis_settings][display_type]' => 'addthis_basic_toolbox', + 'settings[addthis_settings][type_settings][basic_toolbox][share_services]' => 'facebook,twitter,!@#@!$%!@$@!$', + ]; + $this->drupalPostForm('admin/structure/block/manage/' . $block['id'], $edit, t('Save block')); + $this->assertText(t('The declared services are incorrect or nonexistent.'), 'AddThis block validation failed because services has special characters.'); + + + //Test - validation should pass. + $edit = [ + 'settings[addthis_settings][display_type]' => 'addthis_basic_toolbox', + 'settings[addthis_settings][type_settings][basic_toolbox][share_services]' => 'twitter,facebook,compact', + ]; + $this->drupalPostForm('admin/structure/block/manage/' . $block['id'], $edit, t('Save block')); + $this->assertText(t('The block configuration has been saved.'), 'AddThis block saved.'); + + + } + + + /** + * Tests ability to create a block with "addthis_basic_button" and verifies the form fields. + * - Creates an AddThis block + * - Sets Display type to addthis_basic_button + * - Confirms fields are available + * - Saves block settings + * - Confirms configuration was saved successfully. + * @throws \Exception + */ + public function testAddThisBlockButtonConfig(){ + $block = array(); + $block['id'] = 'addthis_block'; + $block['settings[label]'] = $this->randomMachineName(8); + $block['theme'] = $this->config('system.theme')->get('default'); + $block['region'] = 'content'; + $edit = array( + 'settings[label]' => $block['settings[label]'], + 'id' => $block['id'], + 'region' => $block['region'], + 'settings[addthis_settings][display_type]' => 'addthis_basic_button', + ); + + //Create Block with type=addthis_basic_toolbox + $this->drupalPostForm('admin/structure/block/add/' . $block['id'] . '/' . $block['theme'], $edit, t('Save block')); + $this->assertText(t('The block configuration has been saved.'), 'AddThis block has been created.'); + + //Load up new block. + $this->drupalGet('admin/structure/block/manage/' . $block['id']); + //Verify form fields for toolbox are here. + $this->assertFieldByName('settings[addthis_settings][type_settings][basic_button][button_size]'); + $this->assertFieldByName('settings[addthis_settings][type_settings][basic_button][extra_css]'); + + //Verify block config saves and loads properly + $edit = array( + 'settings[addthis_settings][display_type]' => 'addthis_basic_button', + 'settings[addthis_settings][type_settings][basic_button][button_size]' => 'big', + 'settings[addthis_settings][type_settings][basic_button][extra_css]' => $this->randomMachineName(), + ); + $this->drupalPostForm('admin/structure/block/manage/' . $block['id'], $edit, t('Save block')); + //Load up the new block configs. + $this->drupalGet('admin/structure/block/manage/' . $block['id']); + $this->assertRaw($edit['settings[addthis_settings][display_type]'], 'Content of configurable type successfully verified.'); + $this->assertRaw($edit['settings[addthis_settings][type_settings][basic_button][button_size]'], 'Content of configurable button size block successfully verified.'); + $this->assertRaw($edit['settings[addthis_settings][type_settings][basic_button][extra_css]'], 'Content of configurable extra css block successfully verified.'); + + + } + + + +} \ No newline at end of file diff --git a/src/Tests/AddThisFieldTest.php b/src/Tests/AddThisFieldTest.php new file mode 100644 index 0000000..18e40c8 --- /dev/null +++ b/src/Tests/AddThisFieldTest.php @@ -0,0 +1,124 @@ +adminUser = $this->drupalCreateUser(array_keys(\Drupal::service('user.permissions')->getPermissions())); + $this->drupalLogin($this->adminUser); + + } + /** + * Tests ability to add the addthis field to a node. + * - Creates content type + * - Adds AddThis field + */ + public function testAddThisAddFieldToNode() { + $this->fieldName = $this->createField('addthis', 'addthis_button_widget', '1'); + } + + /** + * Tests the Manage Form settings for the AddThis Toolbox + * - Creates content type + * - Adds AddThis field + * - Visits Manage Form Page + * - Sets display type to "AddThis Basic Toolbox" + * - Verifies form fields. + * - @TODO Confirm saving settings & confrim + */ + public function testAddThisFieldToolboxWidgetForm(){ + $this->fieldName = $this->createField('addthis', 'addthis_button_widget', '1'); + $this->drupalGet('admin/structure/types/manage/'. $this->contentTypeName . '/display'); + $this->assertText($this->fieldName, 'Field is configurable'); + + //Fieldname is set to the name, without field_ so we need to add it back. + $field_key = 'field_' . $this->fieldName; + + //Set the display type to AddThis Toolbox. + $format = 'addthis_basic_toolbox'; + $edit = array( + 'fields[' . $field_key. '][type]' => $format, + 'refresh_rows' => $field_key + ); + $this->drupalPostAjaxForm(NULL, $edit, array('op' => t('Refresh'))); + $this->assertFieldByName('fields[' . $field_key. '][type]', $format, 'The expected formatter is selected.'); + + // Click on the formatter settings button to open the formatter settings + // form. + $this->drupalPostAjaxForm(NULL, array(), $field_key . '_settings_edit'); + + $this->assertFieldByName('fields[' . $field_key . '][settings_edit_form][settings][share_services]'); + $this->assertFieldByName('fields[' . $field_key . '][settings_edit_form][settings][buttons_size]'); + $this->assertFieldByName('fields[' . $field_key . '][settings_edit_form][settings][counter_orientation]'); + $this->assertFieldByName('fields[' . $field_key . '][settings_edit_form][settings][extra_css]'); + + } + + /** + * Tests the Manage Form settings for the AddThis Button + * - Creates content type + * - Adds AddThis field + * - Visits Manage Form Page + * - Sets display type to "AddThis Basic Button" + * - Verifies form fields. + * - @TODO Confirm saving settings & confrim + */ + public function testAddThisFieldButtonWidgetForm(){ + $this->fieldName = $this->createField('addthis', 'addthis_button_widget', '1'); + $this->drupalGet('admin/structure/types/manage/'. $this->contentTypeName . '/display'); + $this->assertText($this->fieldName, 'Field is configurable'); + + //Fieldname is set to the name, without field_ so we need to add it back. + $field_key = 'field_' . $this->fieldName; + + //Set the display type to AddThis Toolbox. + $format = 'addthis_basic_button'; + $edit = array( + 'fields[' . $field_key. '][type]' => $format, + 'refresh_rows' => $field_key + ); + $this->drupalPostAjaxForm(NULL, $edit, array('op' => t('Refresh'))); + $this->assertFieldByName('fields[' . $field_key. '][type]', $format, 'The expected formatter is selected.'); + + // Click on the formatter settings button to open the formatter settings + // form. + $this->drupalPostAjaxForm(NULL, array(), $field_key . '_settings_edit'); + + $this->assertFieldByName('fields[' . $field_key . '][settings_edit_form][settings][button_size]'); + $this->assertFieldByName('fields[' . $field_key . '][settings_edit_form][settings][extra_css]'); + + } + + + + + +} \ No newline at end of file diff --git a/src/Tests/AddThisFieldWebTestBase.php b/src/Tests/AddThisFieldWebTestBase.php new file mode 100644 index 0000000..5d79b49 --- /dev/null +++ b/src/Tests/AddThisFieldWebTestBase.php @@ -0,0 +1,151 @@ +administratorAccount = $this->drupalCreateUser($permissions); + parent::drupalLogin($this->administratorAccount); + + // Prepare a new content type where the field will be added. + $this->contentTypeName = strtolower($this->randomMachineName(10)); + $this->drupalGet('admin/structure/types/add'); + $edit = array( + 'name' => $this->contentTypeName, + 'type' => $this->contentTypeName, + ); + $this->drupalPostForm(NULL, $edit, t('Save and manage fields')); + $this->assertText(t('The content type @name has been added.', array('@name' => $this->contentTypeName))); + + // Reset the permission cache. + $create_permission = 'create ' . $this->contentTypeName . ' content'; + $this->checkPermissions(array($create_permission), TRUE); + + // Now that we have a new content type, create a user that has privileges + // on the content type. + $this->authorAccount = $this->drupalCreateUser(array($create_permission)); + } + + /** + * Create a field on the content type created during setUp(). + * + * @param string $type + * The storage field type to create + * @param string $widget_type + * The widget to use when editing this field + * @param int|string $cardinality + * Cardinality of the field. Use -1 to signify 'unlimited.' + * + * @return string + * Name of the field, like field_something + */ + protected function createField($type = 'field_example_rgb', $widget_type = 'field_example_text', $cardinality = '1') { + $this->drupalGet('admin/structure/types/manage/' . $this->contentTypeName . '/fields'); + + // Go to the 'Add field' page. + $this->clickLink('Add field'); + + // Make a name for this field. + $field_name = strtolower($this->randomMachineName(10)); + + // Fill out the field form. + $edit = array( + 'new_storage_type' => $type, + 'field_name' => $field_name, + 'label' => $field_name, + ); + $this->drupalPostForm(NULL, $edit, t('Save and continue')); + + // Fill out the $cardinality form as if we're not using an unlimited number + // of values. + $edit = array( + 'cardinality' => 'number', + 'cardinality_number' => (string) $cardinality, + ); + // If we have -1 for $cardinality, we should change the form's drop-down + // from 'Number' to 'Unlimited.' + if (-1 == $cardinality) { + $edit = array( + 'cardinality' => '-1', + 'cardinality_number' => '1', + ); + } + + // And now we save the cardinality settings. + $this->drupalPostForm(NULL, $edit, t('Save field settings')); + debug( + t('Saved settings for field %field_name with widget %widget_type and cardinality %cardinality', + array( + '%field_name' => $field_name, + '%widget_type' => $widget_type, + '%cardinality' => $cardinality, + ) + ) + ); + $this->assertText(t('Updated field @name field settings.', array('@name' => $field_name))); + + // Set the widget type for the newly created field. + $this->drupalGet('admin/structure/types/manage/' . $this->contentTypeName . '/form-display'); + $edit = array( + 'fields[field_' . $field_name . '][type]' => $widget_type, + ); + $this->drupalPostForm(NULL, $edit, t('Save')); + + return $field_name; + } + +} diff --git a/src/Tests/AddThisPermissionsTest.php b/src/Tests/AddThisPermissionsTest.php new file mode 100644 index 0000000..5621320 --- /dev/null +++ b/src/Tests/AddThisPermissionsTest.php @@ -0,0 +1,63 @@ +drupalCreateUser(array('administer site configuration')); + $this->drupalLogin($user1); + + $this->drupalGet('admin/config/user-interface/addthis'); + $this->assertRaw(t('Access denied'), + 'A user without administer addthis permission should not be able to access AddThis system settings.'); + + $this->drupalGet('admin/config/user-interface/addthis/advanced'); + $this->assertRaw(t('Access denied'), + 'A user without administer advanced addthis permission should not be able to access AddThis system settings.'); + } + + function testAddThisConfigPermissionGranted(){ + // Create user with permission to "administer addthis settings". + $user1 = $this->drupalCreateUser(array('administer addthis settings')); + $this->drupalLogin($user1); + + $this->drupalGet('admin/config/user-interface/addthis'); + //$this->assertNoText(t('Access denied'), 'A user with administer addthis should be able to access the basic addthis configuration page'); + + $this->drupalGet('admin/config/user-interface/addthis/advanced'); + $this->assertText(t('Access denied'), + 'A user without administer advanced addthis permission should not be able to access AddThis system settings.'); + } + + function testAddThisConfigPermissionAdvancedGranted(){ + // Create user with permission to "administer advanced addthis settings". + $user1 = $this->drupalCreateUser(array('administer advanced addthis settings')); + $this->drupalLogin($user1); + + $this->drupalGet('admin/config/user-interface/addthis/advanced'); + $this->assertNoRaw(t('Access denied'), + 'A user with administer advanced addthis permission should be able to access AddThis system settings.'); + } + + +} \ No newline at end of file From 70951f9685ee5a97ab445b764bfeecd01767ee67 Mon Sep 17 00:00:00 2001 From: John Doyle Date: Thu, 27 Aug 2015 16:16:46 -0400 Subject: [PATCH 71/77] Added comments to permissions test --- src/Tests/AddThisPermissionsTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Tests/AddThisPermissionsTest.php b/src/Tests/AddThisPermissionsTest.php index 5621320..81364c5 100644 --- a/src/Tests/AddThisPermissionsTest.php +++ b/src/Tests/AddThisPermissionsTest.php @@ -22,6 +22,11 @@ class AddThisPermissionsTest extends WebTestBase { */ public static $modules = array('addthis', 'block', 'field', 'dblog'); + /** + * Verifies that users cannot access addthis pages without permissions + * - Creates a new user with administer site permissions + * - verifies user cannot access addthis settings. + */ function testAddThisConfigPermissionDenied(){ // Create user with permission to "administer addthis settings". $user1 = $this->drupalCreateUser(array('administer site configuration')); @@ -36,6 +41,12 @@ function testAddThisConfigPermissionDenied(){ 'A user without administer advanced addthis permission should not be able to access AddThis system settings.'); } + /** + * Verifies that users can access addthis basic settings page with + * administer addthis settings permission + * - Creates a new user with administer addthis settings + * - verifies user can access addthis basic settings form. + */ function testAddThisConfigPermissionGranted(){ // Create user with permission to "administer addthis settings". $user1 = $this->drupalCreateUser(array('administer addthis settings')); @@ -49,6 +60,12 @@ function testAddThisConfigPermissionGranted(){ 'A user without administer advanced addthis permission should not be able to access AddThis system settings.'); } + /** + * Verifies that users can access addthis advanced settings with + * administer advanced addthis settings permission. + * - Creates a new user with dminister advanced addthis settings + * - verifies user can access addthis advanced settings. + */ function testAddThisConfigPermissionAdvancedGranted(){ // Create user with permission to "administer advanced addthis settings". $user1 = $this->drupalCreateUser(array('administer advanced addthis settings')); From 0112132bfe498b8131b6f1899aa0610634156f6c Mon Sep 17 00:00:00 2001 From: John Doyle Date: Wed, 2 Sep 2015 12:00:55 -0400 Subject: [PATCH 72/77] Made permissions more specific for tests --- src/Tests/AddThisBlockTest.php | 7 +++++-- src/Tests/AddThisFieldTest.php | 21 ++++++--------------- src/Tests/AddThisFieldWebTestBase.php | 3 ++- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/Tests/AddThisBlockTest.php b/src/Tests/AddThisBlockTest.php index 4e224b7..e751b40 100644 --- a/src/Tests/AddThisBlockTest.php +++ b/src/Tests/AddThisBlockTest.php @@ -20,7 +20,7 @@ class AddThisBlockTest extends WebTestBase { * * @var array */ - public static $modules = array('addthis', 'block', 'node', 'field', 'dblog'); + public static $modules = array('addthis', 'block'); /** * User account with all available permissions @@ -34,7 +34,9 @@ class AddThisBlockTest extends WebTestBase { */ protected function setUp(){ parent::setUp(); - $this->adminUser = $this->drupalCreateUser(array_keys(\Drupal::service('user.permissions')->getPermissions())); + $this->adminUser = $this->drupalCreateUser([ + 'administer blocks', + ]); $this->drupalLogin($this->adminUser); } /** @@ -97,6 +99,7 @@ public function testAddThisBlockToolboxConfig(){ 'settings[addthis_settings][type_settings][basic_toolbox][extra_css]' => $this->randomMachineName(), ]; $this->drupalPostForm('admin/structure/block/manage/' . $block['id'], $edit, t('Save block')); + $this->drupalGet('admin/structure/block/manage/' . $block['id']); $this->assertRaw($edit['settings[addthis_settings][display_type]'], 'Content of configurable type successfully verified.'); $this->assertRaw($edit['settings[addthis_settings][type_settings][basic_toolbox][share_services]'], 'Content of configurable share services block successfully verified.'); $this->assertRaw($edit['settings[addthis_settings][type_settings][basic_toolbox][buttons_size]'], 'Content of configurable button size block successfully verified.'); diff --git a/src/Tests/AddThisFieldTest.php b/src/Tests/AddThisFieldTest.php index 18e40c8..81d16ab 100644 --- a/src/Tests/AddThisFieldTest.php +++ b/src/Tests/AddThisFieldTest.php @@ -19,29 +19,16 @@ class AddThisFieldTest extends AddThisFieldWebTestBase { */ public static $modules = array('addthis', 'node', 'field', 'dblog'); - /** - * User account with all available permissions - * - * @var \Drupal\Core\Session\AccountInterface - */ - protected $adminUser; - /** - * {@inheritdoc} - */ - protected function setUp(){ - parent::setUp(); - $this->adminUser = $this->drupalCreateUser(array_keys(\Drupal::service('user.permissions')->getPermissions())); - $this->drupalLogin($this->adminUser); - - } /** * Tests ability to add the addthis field to a node. * - Creates content type * - Adds AddThis field */ public function testAddThisAddFieldToNode() { + // Add a single field as administrator user. + $this->drupalLogin($this->administratorAccount); $this->fieldName = $this->createField('addthis', 'addthis_button_widget', '1'); } @@ -55,6 +42,8 @@ public function testAddThisAddFieldToNode() { * - @TODO Confirm saving settings & confrim */ public function testAddThisFieldToolboxWidgetForm(){ + // Add a single field as administrator user. + $this->drupalLogin($this->administratorAccount); $this->fieldName = $this->createField('addthis', 'addthis_button_widget', '1'); $this->drupalGet('admin/structure/types/manage/'. $this->contentTypeName . '/display'); $this->assertText($this->fieldName, 'Field is configurable'); @@ -92,6 +81,8 @@ public function testAddThisFieldToolboxWidgetForm(){ * - @TODO Confirm saving settings & confrim */ public function testAddThisFieldButtonWidgetForm(){ + // Add a single field as administrator user. + $this->drupalLogin($this->administratorAccount); $this->fieldName = $this->createField('addthis', 'addthis_button_widget', '1'); $this->drupalGet('admin/structure/types/manage/'. $this->contentTypeName . '/display'); $this->assertText($this->fieldName, 'Field is configurable'); diff --git a/src/Tests/AddThisFieldWebTestBase.php b/src/Tests/AddThisFieldWebTestBase.php index 5d79b49..f134fed 100644 --- a/src/Tests/AddThisFieldWebTestBase.php +++ b/src/Tests/AddThisFieldWebTestBase.php @@ -41,7 +41,7 @@ class AddThisFieldWebTestBase extends WebTestBase { * * @var array */ - public static $modules = array('node', 'field_ui', 'field_example'); + public static $modules = array('addthis', 'node', 'field_ui', 'field',); /** * {@inheritdoc} @@ -57,6 +57,7 @@ protected function setUp() { 'administer content types', 'administer node fields', 'administer node form display', + 'administer node display', ); $this->administratorAccount = $this->drupalCreateUser($permissions); parent::drupalLogin($this->administratorAccount); From 57dc472376ecde0c5415d8601374549526179adf Mon Sep 17 00:00:00 2001 From: John Doyle Date: Thu, 3 Sep 2015 09:55:46 -0400 Subject: [PATCH 73/77] Stubbed out some functions. Fixed JS typo --- js/addthis.js | 2 +- src/Tests/AddThisBaseTest.php | 47 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/Tests/AddThisBaseTest.php diff --git a/js/addthis.js b/js/addthis.js index 2243ba2..f08e008 100644 --- a/js/addthis.js +++ b/js/addthis.js @@ -23,7 +23,7 @@ var addthis_config, addthis_config = settings.addThisWidget.config; } if (typeof settings.addThisWidget.share !== 'undefined') { - addthis_share = settings.addThisWidget.config; + addthis_share = settings.addThisWidget.share; } } } diff --git a/src/Tests/AddThisBaseTest.php b/src/Tests/AddThisBaseTest.php new file mode 100644 index 0000000..73e9f61 --- /dev/null +++ b/src/Tests/AddThisBaseTest.php @@ -0,0 +1,47 @@ + Date: Tue, 8 Sep 2015 12:17:10 -0400 Subject: [PATCH 74/77] Stubbed out test functions and made some modifications to the class structures. Passing off to Jason --- src/Tests/AddThisBaseTest.php | 25 +++++++++++++++++++++++-- src/Tests/AddThisBlockTest.php | 5 ++--- src/Tests/AddThisFieldTest.php | 8 -------- src/Tests/AddThisFieldWebTestBase.php | 5 ++--- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/Tests/AddThisBaseTest.php b/src/Tests/AddThisBaseTest.php index 73e9f61..bc92c57 100644 --- a/src/Tests/AddThisBaseTest.php +++ b/src/Tests/AddThisBaseTest.php @@ -36,11 +36,32 @@ public function testDrupalSettings(){ } - public function testAddThisToolboxMarkup($services){ + /** + * Helper function to test the toolbox markup on a page. + * - Ensure the container element has the correct AddThis Class + Custom Classes + * - Loop through the services enabled + * - Ensure anchor tag is present with the correct class. + * + * @param $services + * @param $button_size + * @param $orientation + * @param $extra_css + */ + public function testAddThisToolboxMarkup($services, $button_size, $orientation, $extra_css){ } - public function testAddThisButtonMarkup(){ + /** + * Helper function to test the basic button markup on a page. + * - Ensure the container element has the correct AddThis Class + Custom Classes + * - Ensure the correct button size is present. + * + * @param $services + * @param $button_size + * @param $orientation + * @param $extra_css + */ + public function testAddThisButtonMarkup($button_size, $extra_css){ } diff --git a/src/Tests/AddThisBlockTest.php b/src/Tests/AddThisBlockTest.php index e751b40..70d35e8 100644 --- a/src/Tests/AddThisBlockTest.php +++ b/src/Tests/AddThisBlockTest.php @@ -5,14 +5,13 @@ */ namespace Drupal\addthis\Tests; -use Drupal\simpletest\WebTestBase; /** * Tests the basic functionality provided by AddThis. * * @group addthis */ -class AddThisBlockTest extends WebTestBase { +class AddThisBlockTest extends AddThisBaseTest { /** @@ -20,7 +19,7 @@ class AddThisBlockTest extends WebTestBase { * * @var array */ - public static $modules = array('addthis', 'block'); + public static $modules = array('block'); /** * User account with all available permissions diff --git a/src/Tests/AddThisFieldTest.php b/src/Tests/AddThisFieldTest.php index 81d16ab..7bafe2d 100644 --- a/src/Tests/AddThisFieldTest.php +++ b/src/Tests/AddThisFieldTest.php @@ -12,14 +12,6 @@ */ class AddThisFieldTest extends AddThisFieldWebTestBase { - /** - * Modules to enable. - * - * @var array - */ - public static $modules = array('addthis', 'node', 'field', 'dblog'); - - /** * Tests ability to add the addthis field to a node. diff --git a/src/Tests/AddThisFieldWebTestBase.php b/src/Tests/AddThisFieldWebTestBase.php index f134fed..cb4959d 100644 --- a/src/Tests/AddThisFieldWebTestBase.php +++ b/src/Tests/AddThisFieldWebTestBase.php @@ -12,9 +12,8 @@ namespace Drupal\addthis\Tests; use Drupal\Core\Session\AccountInterface; -use Drupal\simpletest\WebTestBase; -class AddThisFieldWebTestBase extends WebTestBase { +class AddThisFieldWebTestBase extends AddThisBaseTest { /** * @var string @@ -41,7 +40,7 @@ class AddThisFieldWebTestBase extends WebTestBase { * * @var array */ - public static $modules = array('addthis', 'node', 'field_ui', 'field',); + public static $modules = array('node', 'field_ui', 'field',); /** * {@inheritdoc} From d72437317cffc6bda60f06bba83d0619f9e264b2 Mon Sep 17 00:00:00 2001 From: Pieter Jonckiere Date: Fri, 9 Oct 2015 22:25:15 +0200 Subject: [PATCH 75/77] Issue 25: remove the addthis class --- addthis.services.yml | 5 -- src/AddThis.php | 73 ------------------- src/AddThisScriptManager.php | 46 ++++++++++++ src/Form/AddThisSettingsAdvancedForm.php | 1 - src/Form/AddThisSettingsForm.php | 28 ++----- .../AddThisBasicButtonFormatter.php | 1 - .../AddThisBasicToolboxFormatter.php | 1 - src/Tests/AddThisBaseTest.php | 3 + 8 files changed, 56 insertions(+), 102 deletions(-) delete mode 100644 src/AddThis.php diff --git a/addthis.services.yml b/addthis.services.yml index b0cea68..1b81925 100644 --- a/addthis.services.yml +++ b/addthis.services.yml @@ -1,9 +1,4 @@ services: - # AddThis service? - addthis.addthis: - class: Drupal\addthis\AddThis - arguments: ['@addthis.script_manager', '@config.factory'] - # This service creates a more complex access check. addthis.script_manager: class: Drupal\addthis\AddThisScriptManager diff --git a/src/AddThis.php b/src/AddThis.php deleted file mode 100644 index d80b115..0000000 --- a/src/AddThis.php +++ /dev/null @@ -1,73 +0,0 @@ -add_this_script_manager = $addThisScriptManager; - $this->config_factory = $configFactory; - $this->json = new AddThisJson(); - $this->setConfig(); - } - - /** - * Set the json object. - */ - public function setJson(AddThisJson $json) { - $this->json = $json; - } - - public function setConfig() { - $this->config = $this->config_factory->get('addthis.settings'); - } - - public function getServices() { - $rows = array(); - $services = $this->json->decode($this->getServicesJsonUrl()); - if (empty($services)) { - drupal_set_message(t('AddThis services could not be loaded from @service_url', array( - '@service_url', - $this->getServicesJsonUrl() - )), 'warning'); - } - else { - foreach ($services['data'] as $service) { - $serviceCode = SafeMarkup::checkPlain($service['code']); - $serviceName = SafeMarkup::checkPlain($service['name']); - $service = array( - '#type' => 'inline_template', - '#template' => ' ' . $serviceName, - ); - //#options expects a string, not an array. Render the element so it becomes a string. - $rows[$serviceCode] = render($service); - } - } - return $rows; - } - - public function getServicesJsonUrl() { - $service_json_url_key = $this->config->get('addthis_services_json_url'); - $service_json_url_key = isset($service_json_url_key) ? $service_json_url_key : 'http://cache.addthiscdn.com/services/v1/sharing.en.json'; - return check_url($service_json_url_key); - } - - -} diff --git a/src/AddThisScriptManager.php b/src/AddThisScriptManager.php index 4a3351c..0529381 100644 --- a/src/AddThisScriptManager.php +++ b/src/AddThisScriptManager.php @@ -19,6 +19,10 @@ namespace Drupal\addthis; +use Drupal\addthis\Util\AddThisJson; +use Drupal\Component\Utility\Html; +use Drupal\Component\Utility\SafeMarkup; +use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Url; class AddThisScriptManager { @@ -128,6 +132,48 @@ protected function getServiceNamesAsCommaSeparatedString($services) { return $servicesAsCommaSeparatedString; } + /** + * Get an array containing the rendered AddThis services. + * + * @return array + * An array containing the rendered AddThis services. + */ + public function getServices() { + $rows = array(); + $json = new AddThisJson(); + $services = $json->decode($this->getServicesJsonUrl()); + if (empty($services)) { + drupal_set_message(t('AddThis services could not be loaded from @service_url', array( + '@service_url', + $this->getServicesJsonUrl() + )), 'warning'); + } + else { + foreach ($services['data'] as $service) { + $serviceCode = SafeMarkup::checkPlain($service['code']); + $serviceName = SafeMarkup::checkPlain($service['name']); + $service = array( + '#type' => 'inline_template', + '#template' => ' ' . $serviceName, + ); + //#options expects a string, not an array. Render the element so it becomes a string. + $rows[$serviceCode] = render($service); + } + } + return $rows; + } + + /** + * Gets the AddThis services url. + * + * @return string + */ + public function getServicesJsonUrl() { + $config = $this->config_factory->get('addthis.settings.advanced'); + $service_json_url_key = $config->get('addthis_services_json_url'); + $service_json_url_key = isset($service_json_url_key) ? $service_json_url_key : 'http://cache.addthiscdn.com/services/v1/sharing.en.json'; + return Html::escape(UrlHelper::stripDangerousProtocols($service_json_url_key)); + } /** * Attach the widget js to the element. diff --git a/src/Form/AddThisSettingsAdvancedForm.php b/src/Form/AddThisSettingsAdvancedForm.php index 2a98785..ae8be16 100644 --- a/src/Form/AddThisSettingsAdvancedForm.php +++ b/src/Form/AddThisSettingsAdvancedForm.php @@ -10,7 +10,6 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Extension\ModuleHandler; use Drupal\Component\Utility\Xss; -use Drupal\addthis\AddThis; /** * Defines a form to configure maintenance settings for this site. diff --git a/src/Form/AddThisSettingsForm.php b/src/Form/AddThisSettingsForm.php index 3fa8ac4..24bf17e 100644 --- a/src/Form/AddThisSettingsForm.php +++ b/src/Form/AddThisSettingsForm.php @@ -6,6 +6,8 @@ namespace Drupal\addthis\Form; +use Drupal\addthis\AddThisScriptManager; +use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Component\Utility\Xss; @@ -15,22 +17,6 @@ */ class AddThisSettingsForm extends ConfigFormBase { - /** - * @var \Drupal\Core\Config\ConfigFactory - */ - protected $config_factory; - - /** - * Construct function. - * - * @param \Drupal\Core\Language\LanguageManager $languageManager - * @param \Drupal\Core\Config\ConfigFactory $configFactory - */ - public function __construct(\Drupal\Core\Config\ConfigFactory $configFactory) { - $this->config_factory = $configFactory; - } - - /** * {@inheritdoc} */ @@ -50,6 +36,8 @@ protected function getEditableConfigNames() { */ public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('addthis.settings'); + $add_this_script_manager = \Drupal::service('addthis.script_manager'); + //Add our library to the settings form to add in custom CSS. $form['#attached']['library'][] = 'addthis/addthis.admin'; @@ -126,12 +114,10 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#open' => FALSE, ); - $add_this_service = \Drupal::service('addthis.addthis'); - $form['compact_menu']['enabled_services']['addthis_enabled_services'] = array( '#type' => 'checkboxes', '#title' => t('Enabled services'), - '#options' => $add_this_service->getServices(), + '#options' => $add_this_script_manager->getServices(), '#default_value' => $config->get('compact_menu.enabled_services.addthis_enabled_services'), '#required' => FALSE, '#columns' => 3, @@ -176,7 +162,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { $form['excluded_services']['addthis_excluded_services'] = array( '#type' => 'checkboxes', '#title' => t('Excluded services'), - '#options' => $add_this_service->getServices(), + '#options' => $add_this_script_manager->getServices(), '#default_value' => $config->get('excluded_services.addthis_excluded_services'), '#required' => FALSE, '#columns' => 3, @@ -251,7 +237,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { $can_do_google_social_tracking = \Drupal::moduleHandler() ->moduleExists('google_analytics'); //@TODO Get back to this. - $google_analytics_config = $this->config_factory->get('google_analytics.settings'); + $google_analytics_config = $this->configFactory()->get('google_analytics.settings'); $google_analytics_account = $google_analytics_config->get('google_analytics_account'); $is_google_analytics_setup = $can_do_google_social_tracking && isset($google_analytics_account); $form['analytics']['google_analytics'] = array( diff --git a/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php index 8e9a983..d7b741c 100644 --- a/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php @@ -10,7 +10,6 @@ use Drupal\Core\Field\FormatterBase; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; -use Drupal\addthis\AddThis; /** diff --git a/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php index 965333d..153c84a 100644 --- a/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php @@ -10,7 +10,6 @@ use Drupal\Core\Field\FormatterBase; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; -use Drupal\addthis\AddThis; /** * Plugin implementation of the 'addthis_basic_toolbox' formatter. diff --git a/src/Tests/AddThisBaseTest.php b/src/Tests/AddThisBaseTest.php index bc92c57..8dc187a 100644 --- a/src/Tests/AddThisBaseTest.php +++ b/src/Tests/AddThisBaseTest.php @@ -9,6 +9,9 @@ use Drupal\simpletest\WebTestBase; +/** + * Tests the add this functionality. + */ class AddThisBaseTest extends WebTestBase { /** From c371a8a969309c4c627d551beb29511b8b784c62 Mon Sep 17 00:00:00 2001 From: Pieter Jonckiere Date: Fri, 9 Oct 2015 22:25:52 +0200 Subject: [PATCH 76/77] Revert "Issue 25: remove the addthis class" This reverts commit d72437317cffc6bda60f06bba83d0619f9e264b2. --- addthis.services.yml | 5 ++ src/AddThis.php | 73 +++++++++++++++++++ src/AddThisScriptManager.php | 46 ------------ src/Form/AddThisSettingsAdvancedForm.php | 1 + src/Form/AddThisSettingsForm.php | 28 +++++-- .../AddThisBasicButtonFormatter.php | 1 + .../AddThisBasicToolboxFormatter.php | 1 + src/Tests/AddThisBaseTest.php | 3 - 8 files changed, 102 insertions(+), 56 deletions(-) create mode 100644 src/AddThis.php diff --git a/addthis.services.yml b/addthis.services.yml index 1b81925..b0cea68 100644 --- a/addthis.services.yml +++ b/addthis.services.yml @@ -1,4 +1,9 @@ services: + # AddThis service? + addthis.addthis: + class: Drupal\addthis\AddThis + arguments: ['@addthis.script_manager', '@config.factory'] + # This service creates a more complex access check. addthis.script_manager: class: Drupal\addthis\AddThisScriptManager diff --git a/src/AddThis.php b/src/AddThis.php new file mode 100644 index 0000000..d80b115 --- /dev/null +++ b/src/AddThis.php @@ -0,0 +1,73 @@ +add_this_script_manager = $addThisScriptManager; + $this->config_factory = $configFactory; + $this->json = new AddThisJson(); + $this->setConfig(); + } + + /** + * Set the json object. + */ + public function setJson(AddThisJson $json) { + $this->json = $json; + } + + public function setConfig() { + $this->config = $this->config_factory->get('addthis.settings'); + } + + public function getServices() { + $rows = array(); + $services = $this->json->decode($this->getServicesJsonUrl()); + if (empty($services)) { + drupal_set_message(t('AddThis services could not be loaded from @service_url', array( + '@service_url', + $this->getServicesJsonUrl() + )), 'warning'); + } + else { + foreach ($services['data'] as $service) { + $serviceCode = SafeMarkup::checkPlain($service['code']); + $serviceName = SafeMarkup::checkPlain($service['name']); + $service = array( + '#type' => 'inline_template', + '#template' => ' ' . $serviceName, + ); + //#options expects a string, not an array. Render the element so it becomes a string. + $rows[$serviceCode] = render($service); + } + } + return $rows; + } + + public function getServicesJsonUrl() { + $service_json_url_key = $this->config->get('addthis_services_json_url'); + $service_json_url_key = isset($service_json_url_key) ? $service_json_url_key : 'http://cache.addthiscdn.com/services/v1/sharing.en.json'; + return check_url($service_json_url_key); + } + + +} diff --git a/src/AddThisScriptManager.php b/src/AddThisScriptManager.php index 0529381..4a3351c 100644 --- a/src/AddThisScriptManager.php +++ b/src/AddThisScriptManager.php @@ -19,10 +19,6 @@ namespace Drupal\addthis; -use Drupal\addthis\Util\AddThisJson; -use Drupal\Component\Utility\Html; -use Drupal\Component\Utility\SafeMarkup; -use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Url; class AddThisScriptManager { @@ -132,48 +128,6 @@ protected function getServiceNamesAsCommaSeparatedString($services) { return $servicesAsCommaSeparatedString; } - /** - * Get an array containing the rendered AddThis services. - * - * @return array - * An array containing the rendered AddThis services. - */ - public function getServices() { - $rows = array(); - $json = new AddThisJson(); - $services = $json->decode($this->getServicesJsonUrl()); - if (empty($services)) { - drupal_set_message(t('AddThis services could not be loaded from @service_url', array( - '@service_url', - $this->getServicesJsonUrl() - )), 'warning'); - } - else { - foreach ($services['data'] as $service) { - $serviceCode = SafeMarkup::checkPlain($service['code']); - $serviceName = SafeMarkup::checkPlain($service['name']); - $service = array( - '#type' => 'inline_template', - '#template' => ' ' . $serviceName, - ); - //#options expects a string, not an array. Render the element so it becomes a string. - $rows[$serviceCode] = render($service); - } - } - return $rows; - } - - /** - * Gets the AddThis services url. - * - * @return string - */ - public function getServicesJsonUrl() { - $config = $this->config_factory->get('addthis.settings.advanced'); - $service_json_url_key = $config->get('addthis_services_json_url'); - $service_json_url_key = isset($service_json_url_key) ? $service_json_url_key : 'http://cache.addthiscdn.com/services/v1/sharing.en.json'; - return Html::escape(UrlHelper::stripDangerousProtocols($service_json_url_key)); - } /** * Attach the widget js to the element. diff --git a/src/Form/AddThisSettingsAdvancedForm.php b/src/Form/AddThisSettingsAdvancedForm.php index ae8be16..2a98785 100644 --- a/src/Form/AddThisSettingsAdvancedForm.php +++ b/src/Form/AddThisSettingsAdvancedForm.php @@ -10,6 +10,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Extension\ModuleHandler; use Drupal\Component\Utility\Xss; +use Drupal\addthis\AddThis; /** * Defines a form to configure maintenance settings for this site. diff --git a/src/Form/AddThisSettingsForm.php b/src/Form/AddThisSettingsForm.php index 24bf17e..3fa8ac4 100644 --- a/src/Form/AddThisSettingsForm.php +++ b/src/Form/AddThisSettingsForm.php @@ -6,8 +6,6 @@ namespace Drupal\addthis\Form; -use Drupal\addthis\AddThisScriptManager; -use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Component\Utility\Xss; @@ -17,6 +15,22 @@ */ class AddThisSettingsForm extends ConfigFormBase { + /** + * @var \Drupal\Core\Config\ConfigFactory + */ + protected $config_factory; + + /** + * Construct function. + * + * @param \Drupal\Core\Language\LanguageManager $languageManager + * @param \Drupal\Core\Config\ConfigFactory $configFactory + */ + public function __construct(\Drupal\Core\Config\ConfigFactory $configFactory) { + $this->config_factory = $configFactory; + } + + /** * {@inheritdoc} */ @@ -36,8 +50,6 @@ protected function getEditableConfigNames() { */ public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('addthis.settings'); - $add_this_script_manager = \Drupal::service('addthis.script_manager'); - //Add our library to the settings form to add in custom CSS. $form['#attached']['library'][] = 'addthis/addthis.admin'; @@ -114,10 +126,12 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#open' => FALSE, ); + $add_this_service = \Drupal::service('addthis.addthis'); + $form['compact_menu']['enabled_services']['addthis_enabled_services'] = array( '#type' => 'checkboxes', '#title' => t('Enabled services'), - '#options' => $add_this_script_manager->getServices(), + '#options' => $add_this_service->getServices(), '#default_value' => $config->get('compact_menu.enabled_services.addthis_enabled_services'), '#required' => FALSE, '#columns' => 3, @@ -162,7 +176,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { $form['excluded_services']['addthis_excluded_services'] = array( '#type' => 'checkboxes', '#title' => t('Excluded services'), - '#options' => $add_this_script_manager->getServices(), + '#options' => $add_this_service->getServices(), '#default_value' => $config->get('excluded_services.addthis_excluded_services'), '#required' => FALSE, '#columns' => 3, @@ -237,7 +251,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { $can_do_google_social_tracking = \Drupal::moduleHandler() ->moduleExists('google_analytics'); //@TODO Get back to this. - $google_analytics_config = $this->configFactory()->get('google_analytics.settings'); + $google_analytics_config = $this->config_factory->get('google_analytics.settings'); $google_analytics_account = $google_analytics_config->get('google_analytics_account'); $is_google_analytics_setup = $can_do_google_social_tracking && isset($google_analytics_account); $form['analytics']['google_analytics'] = array( diff --git a/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php index d7b741c..8e9a983 100644 --- a/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php @@ -10,6 +10,7 @@ use Drupal\Core\Field\FormatterBase; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\addthis\AddThis; /** diff --git a/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php index 153c84a..965333d 100644 --- a/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php @@ -10,6 +10,7 @@ use Drupal\Core\Field\FormatterBase; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\addthis\AddThis; /** * Plugin implementation of the 'addthis_basic_toolbox' formatter. diff --git a/src/Tests/AddThisBaseTest.php b/src/Tests/AddThisBaseTest.php index 8dc187a..bc92c57 100644 --- a/src/Tests/AddThisBaseTest.php +++ b/src/Tests/AddThisBaseTest.php @@ -9,9 +9,6 @@ use Drupal\simpletest\WebTestBase; -/** - * Tests the add this functionality. - */ class AddThisBaseTest extends WebTestBase { /** From 37628eda73f1fed60a961b9991bf6cc98ff3da34 Mon Sep 17 00:00:00 2001 From: Pieter Jonckiere Date: Fri, 9 Oct 2015 22:26:16 +0200 Subject: [PATCH 77/77] Issue 25: remove the addthis class --- addthis.services.yml | 5 -- src/AddThis.php | 73 ------------------- src/AddThisScriptManager.php | 46 ++++++++++++ src/Form/AddThisSettingsAdvancedForm.php | 1 - src/Form/AddThisSettingsForm.php | 28 ++----- .../AddThisBasicButtonFormatter.php | 1 - .../AddThisBasicToolboxFormatter.php | 1 - src/Tests/AddThisBaseTest.php | 3 + 8 files changed, 56 insertions(+), 102 deletions(-) delete mode 100644 src/AddThis.php diff --git a/addthis.services.yml b/addthis.services.yml index b0cea68..1b81925 100644 --- a/addthis.services.yml +++ b/addthis.services.yml @@ -1,9 +1,4 @@ services: - # AddThis service? - addthis.addthis: - class: Drupal\addthis\AddThis - arguments: ['@addthis.script_manager', '@config.factory'] - # This service creates a more complex access check. addthis.script_manager: class: Drupal\addthis\AddThisScriptManager diff --git a/src/AddThis.php b/src/AddThis.php deleted file mode 100644 index d80b115..0000000 --- a/src/AddThis.php +++ /dev/null @@ -1,73 +0,0 @@ -add_this_script_manager = $addThisScriptManager; - $this->config_factory = $configFactory; - $this->json = new AddThisJson(); - $this->setConfig(); - } - - /** - * Set the json object. - */ - public function setJson(AddThisJson $json) { - $this->json = $json; - } - - public function setConfig() { - $this->config = $this->config_factory->get('addthis.settings'); - } - - public function getServices() { - $rows = array(); - $services = $this->json->decode($this->getServicesJsonUrl()); - if (empty($services)) { - drupal_set_message(t('AddThis services could not be loaded from @service_url', array( - '@service_url', - $this->getServicesJsonUrl() - )), 'warning'); - } - else { - foreach ($services['data'] as $service) { - $serviceCode = SafeMarkup::checkPlain($service['code']); - $serviceName = SafeMarkup::checkPlain($service['name']); - $service = array( - '#type' => 'inline_template', - '#template' => ' ' . $serviceName, - ); - //#options expects a string, not an array. Render the element so it becomes a string. - $rows[$serviceCode] = render($service); - } - } - return $rows; - } - - public function getServicesJsonUrl() { - $service_json_url_key = $this->config->get('addthis_services_json_url'); - $service_json_url_key = isset($service_json_url_key) ? $service_json_url_key : 'http://cache.addthiscdn.com/services/v1/sharing.en.json'; - return check_url($service_json_url_key); - } - - -} diff --git a/src/AddThisScriptManager.php b/src/AddThisScriptManager.php index 4a3351c..0529381 100644 --- a/src/AddThisScriptManager.php +++ b/src/AddThisScriptManager.php @@ -19,6 +19,10 @@ namespace Drupal\addthis; +use Drupal\addthis\Util\AddThisJson; +use Drupal\Component\Utility\Html; +use Drupal\Component\Utility\SafeMarkup; +use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Url; class AddThisScriptManager { @@ -128,6 +132,48 @@ protected function getServiceNamesAsCommaSeparatedString($services) { return $servicesAsCommaSeparatedString; } + /** + * Get an array containing the rendered AddThis services. + * + * @return array + * An array containing the rendered AddThis services. + */ + public function getServices() { + $rows = array(); + $json = new AddThisJson(); + $services = $json->decode($this->getServicesJsonUrl()); + if (empty($services)) { + drupal_set_message(t('AddThis services could not be loaded from @service_url', array( + '@service_url', + $this->getServicesJsonUrl() + )), 'warning'); + } + else { + foreach ($services['data'] as $service) { + $serviceCode = SafeMarkup::checkPlain($service['code']); + $serviceName = SafeMarkup::checkPlain($service['name']); + $service = array( + '#type' => 'inline_template', + '#template' => ' ' . $serviceName, + ); + //#options expects a string, not an array. Render the element so it becomes a string. + $rows[$serviceCode] = render($service); + } + } + return $rows; + } + + /** + * Gets the AddThis services url. + * + * @return string + */ + public function getServicesJsonUrl() { + $config = $this->config_factory->get('addthis.settings.advanced'); + $service_json_url_key = $config->get('addthis_services_json_url'); + $service_json_url_key = isset($service_json_url_key) ? $service_json_url_key : 'http://cache.addthiscdn.com/services/v1/sharing.en.json'; + return Html::escape(UrlHelper::stripDangerousProtocols($service_json_url_key)); + } /** * Attach the widget js to the element. diff --git a/src/Form/AddThisSettingsAdvancedForm.php b/src/Form/AddThisSettingsAdvancedForm.php index 2a98785..ae8be16 100644 --- a/src/Form/AddThisSettingsAdvancedForm.php +++ b/src/Form/AddThisSettingsAdvancedForm.php @@ -10,7 +10,6 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Extension\ModuleHandler; use Drupal\Component\Utility\Xss; -use Drupal\addthis\AddThis; /** * Defines a form to configure maintenance settings for this site. diff --git a/src/Form/AddThisSettingsForm.php b/src/Form/AddThisSettingsForm.php index 3fa8ac4..24bf17e 100644 --- a/src/Form/AddThisSettingsForm.php +++ b/src/Form/AddThisSettingsForm.php @@ -6,6 +6,8 @@ namespace Drupal\addthis\Form; +use Drupal\addthis\AddThisScriptManager; +use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Component\Utility\Xss; @@ -15,22 +17,6 @@ */ class AddThisSettingsForm extends ConfigFormBase { - /** - * @var \Drupal\Core\Config\ConfigFactory - */ - protected $config_factory; - - /** - * Construct function. - * - * @param \Drupal\Core\Language\LanguageManager $languageManager - * @param \Drupal\Core\Config\ConfigFactory $configFactory - */ - public function __construct(\Drupal\Core\Config\ConfigFactory $configFactory) { - $this->config_factory = $configFactory; - } - - /** * {@inheritdoc} */ @@ -50,6 +36,8 @@ protected function getEditableConfigNames() { */ public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('addthis.settings'); + $add_this_script_manager = \Drupal::service('addthis.script_manager'); + //Add our library to the settings form to add in custom CSS. $form['#attached']['library'][] = 'addthis/addthis.admin'; @@ -126,12 +114,10 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#open' => FALSE, ); - $add_this_service = \Drupal::service('addthis.addthis'); - $form['compact_menu']['enabled_services']['addthis_enabled_services'] = array( '#type' => 'checkboxes', '#title' => t('Enabled services'), - '#options' => $add_this_service->getServices(), + '#options' => $add_this_script_manager->getServices(), '#default_value' => $config->get('compact_menu.enabled_services.addthis_enabled_services'), '#required' => FALSE, '#columns' => 3, @@ -176,7 +162,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { $form['excluded_services']['addthis_excluded_services'] = array( '#type' => 'checkboxes', '#title' => t('Excluded services'), - '#options' => $add_this_service->getServices(), + '#options' => $add_this_script_manager->getServices(), '#default_value' => $config->get('excluded_services.addthis_excluded_services'), '#required' => FALSE, '#columns' => 3, @@ -251,7 +237,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { $can_do_google_social_tracking = \Drupal::moduleHandler() ->moduleExists('google_analytics'); //@TODO Get back to this. - $google_analytics_config = $this->config_factory->get('google_analytics.settings'); + $google_analytics_config = $this->configFactory()->get('google_analytics.settings'); $google_analytics_account = $google_analytics_config->get('google_analytics_account'); $is_google_analytics_setup = $can_do_google_social_tracking && isset($google_analytics_account); $form['analytics']['google_analytics'] = array( diff --git a/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php index 8e9a983..d7b741c 100644 --- a/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AddThisBasicButtonFormatter.php @@ -10,7 +10,6 @@ use Drupal\Core\Field\FormatterBase; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; -use Drupal\addthis\AddThis; /** diff --git a/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php b/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php index 965333d..153c84a 100644 --- a/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php +++ b/src/Plugin/Field/FieldFormatter/AddThisBasicToolboxFormatter.php @@ -10,7 +10,6 @@ use Drupal\Core\Field\FormatterBase; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; -use Drupal\addthis\AddThis; /** * Plugin implementation of the 'addthis_basic_toolbox' formatter. diff --git a/src/Tests/AddThisBaseTest.php b/src/Tests/AddThisBaseTest.php index bc92c57..8dc187a 100644 --- a/src/Tests/AddThisBaseTest.php +++ b/src/Tests/AddThisBaseTest.php @@ -9,6 +9,9 @@ use Drupal\simpletest\WebTestBase; +/** + * Tests the add this functionality. + */ class AddThisBaseTest extends WebTestBase { /**