You are here

public function ItemShortcode::process in Shortcode 2.0.x

Same name and namespace in other branches
  1. 8 shortcode_basic_tags/src/Plugin/Shortcode/ItemShortcode.php \Drupal\shortcode_basic_tags\Plugin\Shortcode\ItemShortcode::process()

Performs the shortcode processing.

Parameters

array $attributes: Array of attributes.

string $text: The text string to be processed.

string $langcode: The language code of the text to be filtered. Defaults to LANGCODE_NOT_SPECIFIED.

Return value

string The processed text.

Overrides ShortcodeInterface::process

File

shortcode_basic_tags/src/Plugin/Shortcode/ItemShortcode.php, line 22

Class

ItemShortcode
Insert div or span around the text with some css classes.

Namespace

Drupal\shortcode_basic_tags\Plugin\Shortcode

Code

public function process(array $attributes, $text, $langcode = Language::LANGCODE_NOT_SPECIFIED) {

  // Merge with default attributes.
  $attributes = $this
    ->getAttributes([
    'class' => '',
    'id' => '',
    'style' => '',
    // Default element to div.
    'type' => 'div',
  ], $attributes);

  // Only allow allowed types, and replace common shorthands.
  // TODO: Use map.
  // TODO: Allow any type the user enters?
  switch ($attributes['type']) {
    case 's':
    case 'span':
      $attributes['type'] = 'span';
      break;
    case 'd':
    default:
      $attributes['type'] = 'div';
      break;
  }

  // Build element attributes to be used in twig.
  $element_attributes = [
    'class' => $attributes['class'],
    'id' => $attributes['id'],
    'style' => $attributes['style'],
  ];

  // Filter away empty attributes.
  $element_attributes = array_filter($element_attributes);
  $output = [
    '#theme' => 'shortcode_item',
    '#type' => $attributes['type'],
    '#attributes' => $element_attributes,
    '#text' => $text,
  ];
  return $this
    ->render($output);
}