Commit 8fc5a25a authored by micropat's avatar micropat
Browse files

Replace deprecated Twig and Renderer method

Replace the deprecated Twig `spaceless` filter with the `-` whitespace
control modifier on tags.

Replace the deprecated RenderInterface::renderPlain() with
RenderInterface::renderInIsolation() using
DeprecationHelper::backwardsCompatibleCall() for backwards compatibility
with Drupal 10.
parent 151f0f9b
Loading
Loading
Loading
Loading
+25 −4
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
 * Handle AddToAny integration.
 */

use Drupal\Component\Utility\DeprecationHelper;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
@@ -237,7 +238,7 @@ function addtoany_page_attachments(&$page) {
}

/**
 * Generate data for AddToAny buttons for an entity.
 * Generates data for AddToAny buttons for an entity.
 *
 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
 *   The entity object to create the buttons for.
@@ -261,7 +262,7 @@ function addtoany_create_entity_data(ContentEntityInterface $entity, $config = N
}

/**
 * Generate data for AddToAny buttons.
 * Generates data for AddToAny buttons.
 *
 * @param string $url
 *   If present this will be used as the URL.
@@ -280,7 +281,7 @@ function addtoany_create_data($url = NULL, $title = NULL, $config = NULL) {
    $config = \Drupal::config('addtoany.settings');
  }

  $additional_html = rtrim($config->get('additional_html'));
  $additional_html = addtoany_strip_whitespace($config->get('additional_html'));
  $universal_button_placement = $config->get('universal_button_placement');

  $is_front = \Drupal::service('path.matcher')->isFrontPage();
@@ -319,8 +320,15 @@ function addtoany_create_data($url = NULL, $title = NULL, $config = NULL) {

      // Expecting array|string|null from getTitle.
      if (is_array($title)) {
        /** @var \Drupal\Core\Render\RendererInterface $renderer */
        $renderer = \Drupal::service('renderer');
        $title['#allowed_tags'] = [];
        $title = \Drupal::service('renderer')->renderPlain($title);
        $title = DeprecationHelper::backwardsCompatibleCall(
          currentVersion: \Drupal::VERSION,
          deprecatedVersion: '10.3',
          currentCallable: fn() => $renderer->renderInIsolation($title),
          deprecatedCallable: fn() => $renderer->renderPlain($title),
        );
      }
    }

@@ -352,3 +360,16 @@ function addtoany_create_data($url = NULL, $title = NULL, $config = NULL) {

  return $info;
}

/**
 * Removes whitespace between HTML tags for even spacing between buttons.
 *
 * @param string $html
 *   The HTML to strip whitespace from.
 *
 * @return string
 *   The HTML with whitespace removed.
 */
function addtoany_strip_whitespace($html) {
  return preg_replace('/>\s+</m', '><', trim($html));
}
+1 −1
Original line number Diff line number Diff line
@@ -46,7 +46,7 @@ class AddToAnyBlock extends BlockBase {
    }

    $build = [
      '#addtoany_html'              => $block_options['addtoany_html'],
      '#addtoany_html'              => addtoany_strip_whitespace($block_options['addtoany_html']),
      '#link_url'                   => $block_options['link_url'],
      '#link_title'                 => $block_options['link_title'],
      '#button_setting'             => $data['button_setting'],
+1 −1
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ class AddToAnyFollowBlock extends BlockBase {
    $addtoany_html = $this->configuration['addtoany_html'];

    $build = [
      '#addtoany_html' => $addtoany_html,
      '#addtoany_html' => addtoany_strip_whitespace($addtoany_html),
      '#buttons_size' => $buttons_size,
      '#theme' => 'addtoany_follow',
    ];
+7 −9
Original line number Diff line number Diff line
@@ -7,16 +7,14 @@
 * - addtoany_html: HTML for AddToAny buttons.
 * - buttons_size: Size of buttons.
 *
 * Whitespace is removed between tags for even spacing between buttons.
 *
 * @ingroup themeable
 */
#}
{# Remove whitespace between tags for even spacing between buttons. #}
{% apply spaceless %}

<span class="a2a_kit a2a_kit_size_{{ buttons_size }} a2a_follow addtoany_list">
    {% if addtoany_html %}
      {{ addtoany_html|raw }}
    {% endif %}
  {%- if addtoany_html -%}
    {{- addtoany_html|raw -}}
  {%- endif -%}
</span>

{% endapply %}
+22 −25
Original line number Diff line number Diff line
@@ -12,34 +12,31 @@
 * - link_url: Value of URL to share.
 * - link_title: Value of page title to share.
 *
 * Whitespace is removed between tags for even spacing between buttons.
 *
 * @ingroup themeable
 */
#}
{# Remove whitespace between tags for even spacing between buttons. #}
{% apply spaceless %}

  {% if button_setting != 'none' %}
    {% set universal_button %}
{%- if button_setting != 'none' -%}
  {%- set universal_button -%}
    <a class="a2a_dd addtoany_share" href="https://www.addtoany.com/share#url={{ link_url|url_encode }}&amp;title={{ link_title|url_encode }}">
        {% if button_image %}
      {%- if button_image -%}
        <img src="{{ button_image }}" alt="{{ 'Share'|t }}">
        {% endif %}
      {%- endif -%}
    </a>
    {% endset  %}
  {% endif %}
  {%- endset -%}
{%- endif -%}

<span class="a2a_kit a2a_kit_size_{{ buttons_size }} addtoany_list" data-a2a-url="{{ link_url }}" data-a2a-title="{{ link_title }}">
    {% if universal_button_placement == 'before' %}
      {{ universal_button }}
    {% endif %}
  {%- if universal_button_placement == 'before' -%}
    {{- universal_button -}}
  {%- endif -%}

    {% if addtoany_html %}
      {{ addtoany_html|raw }}
    {% endif %}
  {%- if addtoany_html -%}
    {{- addtoany_html|raw -}}
  {%- endif -%}

    {% if universal_button_placement == 'after' %}
      {{ universal_button }}
    {% endif %}
  {%- if universal_button_placement == 'after' -%}
    {{- universal_button -}}
  {%- endif -%}
</span>

{% endapply %}