-<!-- @file Documentation for the @BootstrapPrerender annotated discovery plugin. -->
+<!-- @file Documentation for the @BootstrapPrerender annotated plugin. -->
<!-- @defgroup -->
<!-- @ingroup -->
# @BootstrapPrerender
render array is built, like the following:
```php
+<?php
$build['my_button'] = [
'#type' => 'link',
'#title' => t('Download'),
- '#url' => \Drupal\Core\Url::fromUserInput('/download', ['query' => ['item' => '1234']]),
+ '#url' => Url::fromUserInput('/download', [
+ 'query' => ['item' => '1234'],
+ ]),
'#context' => [
'downloadButton' => TRUE,
],
];
+?>
```
Replace all following instances of `THEMENAME` with the actual machine name of
following contents:
```php
-/**
- * @file
- * Contains \Drupal\THEMENAME\Plugin\Prerender\Link.
- */
+<?php
namespace Drupal\THEMENAME\Plugin\Prerender;
-use Drupal\bootstrap\Annotation\BootstrapConstant;
-use Drupal\bootstrap\Annotation\BootstrapPrerender;
+use Drupal\bootstrap\Plugin\Prerender\Link as BootstrapLink;
use Drupal\bootstrap\Bootstrap;
use Drupal\bootstrap\Utility\Element;
*
* @see \Drupal\Core\Render\Element\Link::preRenderLink()
*/
-class Link extends \Drupal\bootstrap\Plugin\Prerender\Link {
-
- // It should be noted that you do not need both methods here.
- // This is to just show you the different examples of how this plugin
- // works and how it can be tailored to your needs.
+class Link extends BootstrapLink {
+ /*
+ * It should be noted that you do not need both methods here.
+ * This is to just show you the different examples of how this plugin
+ * works and how it can be tailored to your needs.
+ */
/**
* {@inheritdoc}
$element['#attributes']['class'][] = 'btn-lg';
}
- // You must always return the element in this method, as well as call
- // the parent method when sub-classing this method. It invokes preRenderElement().
+ // You must always return the element in this method, as well as call the
+ // parent method when sub-classing this method as it is used to invoke
+ // static::preRenderElement().
return parent::preRender($element);
}
* {@inheritdoc}
*/
public static function preRenderElement(Element $element) {
- $context = $element->getProperty('context', []);
-
// Make downloadButton links into buttons.
// Same as above, just a little cleaner with the Element utility class.
- if (!empty($context['downloadButton'])) {
+ if ($element->getContext('downloadButton')) {
$element->addClass(['btn', 'btn-primary', 'btn-lg'])->setIcon(Bootstrap::glyphicon('download-alt'));
}
}
}
-
+?>
```
## Rebuild the cache {#rebuild}
click the `Clear all caches` button. Or if you prefer, run `drush cr` from the
command line.
-VoilĂ ! After this, you should have a fully functional `@BootstrapPrerender` plugin!
+VoilĂ ! After this, you should have a fully functional `@BootstrapPrerender`
+plugin!