Commit c77dda76 authored by Ben Myrgorod's avatar Ben Myrgorod Committed by Vishnukumar Pv
Browse files

Issue #3313964 by dealancer: Allow to override template

parent 5dff3669
Loading
Loading
Loading
Loading
+8 −4
Changes for css/tweetabletext.css: 8 added lines, 4 removed lines.
Original line number Diff line number Diff line
a.tweetabletext {
  background: #f9ef79;
  padding: 2px 5px;
  padding: 0 9px 2px 5px;
  border-radius: 4px;
  margin-right: 10px;
  position: relative;
  text-decoration: none;
  border-bottom: transparent;
@@ -9,8 +10,11 @@ span#tweetablelogo {
  content: '';
  background: url('../icons/tweetable_front.svg');
  background-size: 100%;
  width: 15px;
  height: 15px;
  width: 20px;
  height: 20px;
  position: absolute;
  display: inline-block;
  margin: 0 4px;
  top: -5px;
  right: -19px;
}
+21 −6
Changes for js/tweetable.js: 21 added lines, 6 removed lines.
Original line number Diff line number Diff line
/* global drupalSettings, jQuery */
/* eslint no-undef: "error" */
(function ($) {
  // Helper function to interpolate template.
  function interpolate(str, obj) {
    let parts = str.split(/\$\{(?!\d)[\wæøåÆØÅ]*\}/);
    let args = str.match(/[^{\}]+(?=})/g) || [];
    let parameters = args.map(argument => obj[argument] || (obj[argument] === undefined ? "" : obj[argument]));
    return String.raw({ raw: parts }, ...parameters);
  }
  $('span.tweetabletext').each(function (i, e) {
    var text = $(e).text();
    var tweet = $(e).attr('data-tweet');
@@ -20,12 +27,20 @@
      var excess = totalLength - (240 + 3)
      tweet = tweet.substring(0, tweet.length - excess) + '...';
    }
    var template = '';
    var tweetablelogo = '<span id="tweetablelogo"></span>';
    template = '<a target="_blank" class="tweetabletext" href="https://twitter.com/intent/tweet?text=' + tweet + '%0A&url=' + window.location.href + '&hashtags=' + tweetHash + '">' + text + tweetablelogo + '</a>';
    template = $.parseHTML(template);
    if (drupalSettings.tweetable_text_ckeditor.color) {
      $(template).css('background', drupalSettings.tweetable_text_ckeditor.color);
    const replacements = {
      tweet_text: tweet,
      page_url: window.location.href,
      hash_tags: tweetHash,
      display_text: text,
      tweetable_logo: '<span id="tweetablelogo"></span>',
    };
      
    var template = drupalSettings.tweetable_text_ckeditor.template;
    template = $.parseHTML((interpolate(template, replacements)));
      
    var color = drupalSettings.tweetable_text_ckeditor.color;
    if (color) {
      $(template).css('background', color);
    }
    $(e).html(template)
  })
+3 −2
Changes for libraries/css/tweetabletext.css: 3 added lines, 2 removed lines.
Original line number Diff line number Diff line
.tweetabletext {
  margin: 0;
  padding: 0;
  background-color: #4099ff;
  padding: 1px 6px;
  border-radius: 3px;
  background-color: #0bbdf1;
  text-decoration: none;
  color: #ffffff;
}
+14 −2
Changes for src/Form/TweetabletextSettingsForm.php: 14 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ namespace Drupal\tweetable_text_ckeditor\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\Xss;

/**
 * Configure example settings for this site.
@@ -38,13 +39,23 @@ class TweetabletextSettingsForm extends ConfigFormBase {
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config(static::SETTINGS);
    $color = $config->get('tweetable_text_ckeditor_color');
    // $color = $config->get('tweetable_text_ckeditor_color');
    $form['tweetable_text_ckeditor_color'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Tweetable text color'),
      '#default_value' => ($color) ? $color : '#f9ef79',
      '#default_value' => $config->get('tweetable_text_ckeditor_color'),
      '#description' => 'Leave blank for none.',
    ];

    $form['tweetable_text_ckeditor_template'] = array(
      '#type' => 'textarea',
      '#title' => t('Template'),
      '#default_value' => $config->get('tweetable_text_ckeditor_template'),
      '#description' => 'Use following variables: <i>${tweet_text}</i>, <i>${page_url}</i>, <i>${hash_tags}</i>, <i>${display_text}</i>, <i>${tweetable_logo}</i>.',
      '#rows' => 4,
      '#required' => TRUE,
    );

    return parent::buildForm($form, $form_state);
  }

@@ -56,6 +67,7 @@ class TweetabletextSettingsForm extends ConfigFormBase {
    $this->configFactory->getEditable(static::SETTINGS)
      // Set the submitted configuration setting.
      ->set('tweetable_text_ckeditor_color', $form_state->getValue('tweetable_text_ckeditor_color'))
      ->set('tweetable_text_ckeditor_template', Xss::filter($form_state->getValue('tweetable_text_ckeditor_template')))
      ->save();

    parent::submitForm($form, $form_state);
+1
Changes for tweetable_text_ckeditor.info.yml: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -6,3 +6,4 @@ dependencies:
  - ckeditor
# Information added by Drupal.org packaging script on 2019-02-07
core_version_requirement: ^9.3 || ^10
configure: tweetabletext.settings
Loading