public function ClearShortcode::process in Shortcode 2.0.x
Same name and namespace in other branches
- 8 shortcode_basic_tags/src/Plugin/Shortcode/ClearShortcode.php \Drupal\shortcode_basic_tags\Plugin\Shortcode\ClearShortcode::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/ ClearShortcode.php, line 22
Class
- ClearShortcode
- Insert div or span around the text with some css classes.
Namespace
Drupal\shortcode_basic_tags\Plugin\ShortcodeCode
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);
$class = $this
->addClass($attributes['class'], 'clearfix');
// 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' => $class,
'id' => $attributes['id'],
'style' => $attributes['style'],
];
// Filter away empty attributes.
$element_attributes = array_filter($element_attributes);
$output = [
'#theme' => 'shortcode_clear',
'#type' => $attributes['type'],
'#attributes' => $element_attributes,
'#text' => $text,
];
return $this
->render($output);
}