RandomShortcode.php in Shortcode 2.0.x
File
shortcode_basic_tags/src/Plugin/Shortcode/RandomShortcode.php
View source
<?php
namespace Drupal\shortcode_basic_tags\Plugin\Shortcode;
use Drupal\Core\Language\Language;
use Drupal\shortcode\Plugin\ShortcodeBase;
class RandomShortcode extends ShortcodeBase {
public function process(array $attributes, $text, $langcode = Language::LANGCODE_NOT_SPECIFIED) {
$attributes = $this
->getAttributes([
'length' => 8,
], $attributes);
$length = intval($attributes['length']);
$length = max($length, 8);
$length = min($length, 99);
$text = '';
for ($i = 0; $i < $length; ++$i) {
$text .= chr(rand(32, 126));
}
return $text;
}
public function tips($long = FALSE) {
$output = [];
$output[] = '<p><strong>[random (length="8") /]</strong>';
$output[] = $this
->t('Inserts a random text with the given length.') . '</p>';
return implode(' ', $output);
}
}