public function ButtonShortcode::process in Shortcode 2.0.x
Same name and namespace in other branches
- 8 shortcode_basic_tags/src/Plugin/Shortcode/ButtonShortcode.php \Drupal\shortcode_basic_tags\Plugin\Shortcode\ButtonShortcode::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/ ButtonShortcode.php, line 22
Class
- ButtonShortcode
- The image shortcode.
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([
'path' => '<front>',
'url' => '',
'title' => '',
'class' => '',
'id' => '',
'style' => '',
'media_file_url' => FALSE,
], $attributes);
$url = $attributes['url'];
if (empty($url)) {
$url = $this
->getUrlFromPath($attributes['path'], $attributes['media_file_url']);
}
$title = $this
->getTitleFromAttributes($attributes['title'], $text);
$class = $this
->addClass($attributes['class'], 'button');
// Build element attributes to be used in twig.
$element_attributes = [
'href' => $url,
'class' => $class,
'id' => $attributes['id'],
'style' => $attributes['style'],
'title' => $title,
];
// Filter away empty attributes.
$element_attributes = array_filter($element_attributes);
$output = [
'#theme' => 'shortcode_button',
// Not required for rendering, just for extra context.
'#url' => $url,
'#attributes' => $element_attributes,
'#text' => $text,
];
return $this
->render($output);
}