5 * Hooks and API provided by the Blazy module.
9 * @defgroup blazy_api Blazy API
11 * Information about the Blazy usages.
13 * Modules may implement any of the available hooks to interact with Blazy.
14 * Blazy may be configured using the web interface using formatters, or Views.
15 * However below is a few sample coded ones as per Blazy RC2+.
17 * A single image sample.
19 * function my_module_render_blazy() {
21 * // URI is stored in #settings property so to allow traveling around video
22 * // and lightboxes before being passed into theme_blazy().
23 * 'uri' => 'public://logo.jpg',
25 * // Explicitly request for Blazy.
26 * // This allows Slick lazyLoad to not load Blazy.
29 * // Optionally provide an image style:
30 * 'image_style' => 'thumbnail',
34 * '#theme' => 'blazy',
35 * '#settings' => $settings,
37 * // Or below for clarity:
38 * '#settings' => ['uri' => 'public://logo.jpg', 'lazy' => 'blazy'],
40 * // Pass custom attributes into the same #item_attributes property as
41 * // Blazy formatters so to respect external modules like RDF, etc. without
42 * // extra property. The regular #attributes property is reserved by Blazy
43 * // container which holds either IMG, icons, or iFrame. Meaning Blazy is
45 * '#item_attributes' => [
46 * 'alt' => t('Thumbnail'),
47 * 'title' => t('Thumbnail title'),
51 * // Finally load the library, or include it into a parent container.
52 * '#attached' => ['library' => ['blazy/load']],
58 * @see \Drupal\blazy\Blazy::buildAttributes()
59 * @see \Drupal\blazy\Dejavu\BlazyDefault::imageSettings()
61 * A multiple image sample.
63 * For advanced usages with multiple images, and a few Blazy features such as
64 * lightboxes, lazyloaded images, or iframes, including CSS background and
66 * o Invoke blazy.manager, and or blazy.formatter.manager, services.
67 * o Use \Drupal\blazy\BlazyManager::getImage() method to work with images and
68 * pass relevant settings which request for particular Blazy features
70 * o Use \Drupal\blazy\BlazyManager::attach() to load relevant libraries.
72 * function my_module_render_blazy_multiple() {
73 * // Invoke the plugin class, or use a DI service container accordingly.
74 * $manager = \Drupal::service('blazy.manager');
77 * // Explicitly request for Blazy library.
78 * // This allows Slick lazyLoad, or text formatter, to not load Blazy.
81 * // Supported media switcher options dependent on available modules:
82 * // colorbox, media (Image to iframe), photobox.
83 * 'media_switch' => 'media',
88 * // Load images via $manager->getImage().
89 * // See above ...Formatter::buildElements() for consistent samples.
92 * // Finally attach libraries as requested via $settings.
93 * $build['#attached'] = $manager->attach($settings);
98 * @see \Drupal\blazy\Plugin\Field\FieldFormatter\BlazyFormatterTrait::buildElements()
99 * @see \Drupal\blazy\Plugin\Field\FieldFormatter\BlazyVideoFormatter::buildElements()
100 * @see \Drupal\gridstack\Plugin\Field\FieldFormatter\GridStackFileFormatterBase::buildElements()
101 * @see \Drupal\slick\Plugin\Field\FieldFormatter\SlickFileFormatterBase::buildElements()
102 * @see \Drupal\blazy\BlazyManager::getImage()
103 * @see \Drupal\blazy\Dejavu\BlazyDefault::imageSettings()
106 * Pre-render callback sample to modify/ extend Blazy output.
108 * function my_module_pre_render(array $image) {
109 * $settings = isset($image['#settings']) ? $image['#settings'] : [];
111 * // Video's HREF points to external site, adds URL to local image.
112 * if (!empty($settings['box_url']) && !empty($settings['embed_url'])) {
113 * $image['#url_attributes']['data-box-url'] = $settings['box_url'];
119 * @see hook_blazy_alter()
129 * Alters Blazy attachments to add own library, drupalSettings, and JS template.
132 * The array of loaded library being modified.
133 * @param array $settings
134 * The available array of settings.
138 function hook_blazy_attach_alter(array &$load, array $settings = []) {
139 if (!empty($settings['photoswipe'])) {
140 $load['library'][] = 'my_module/load';
142 $manager = \Drupal::service('blazy.manager');
143 $template = ['#theme' => 'photoswipe_container'];
144 $load['drupalSettings']['photoswipe'] = [
145 'options' => $manager->configLoad('options', 'photoswipe.settings'),
146 'container' => $manager->getRenderer()->renderPlain($template),
152 * Alters available lightboxes for Media switch select option at Blazy UI.
154 * @param array $lightboxes
155 * The array of lightbox options being modified.
157 * @see https://www.drupal.org/project/blazy_photoswipe
161 function hook_blazy_lightboxes_alter(array &$lightboxes) {
162 $lightboxes[] = 'photoswipe';
166 * Alters Blazy output to support a custom lightbox.
168 * @param array $image
169 * The renderable array of image being modified.
170 * @param array $settings
171 * The available array of settings.
175 function hook_blazy_alter(array &$image, array $settings = []) {
176 if (!empty($settings['media_switch']) && $settings['media_switch'] == 'photoswipe') {
177 $image['#pre_render'][] = 'my_module_pre_render';
182 * @} End of "addtogroup hooks".