function MediaLibraryHooks::viewsPreRender

Implements hook_views_pre_render().

File

core/modules/media_library/src/Hook/MediaLibraryHooks.php, line 119

Class

MediaLibraryHooks
Hook implementations for media_library.

Namespace

Drupal\media_library\Hook

Code

public function viewsPreRender(ViewExecutable $view) {
    $add_classes = function (&$option, array $classes_to_add) {
        $classes = $option ? preg_split('/\\s+/', trim($option)) : [];
        $classes = array_filter($classes);
        $classes = array_merge($classes, $classes_to_add);
        $option = implode(' ', array_unique($classes));
    };
    if ($view->id() === 'media_library') {
        if ($view->current_display === 'page') {
            $add_classes($view->style_plugin->options['row_class'], [
                'js-media-library-item',
                'js-click-to-select',
            ]);
            if (array_key_exists('media_bulk_form', $view->field)) {
                $add_classes($view->field['media_bulk_form']->options['element_class'], [
                    'js-click-to-select-checkbox',
                ]);
            }
        }
        elseif (str_starts_with($view->current_display, 'widget')) {
            if (array_key_exists('media_library_select_form', $view->field)) {
                $add_classes($view->field['media_library_select_form']->options['element_wrapper_class'], [
                    'js-click-to-select-checkbox',
                ]);
            }
            $add_classes($view->display_handler->options['css_class'], [
                'js-media-library-view',
            ]);
        }
        $add_classes($view->style_plugin->options['row_class'], [
            'js-media-library-item',
            'js-click-to-select',
        ]);
        if ($view->display_handler->options['defaults']['css_class']) {
            $add_classes($view->displayHandlers
                ->get('default')->options['css_class'], [
                'js-media-library-view',
            ]);
        }
        else {
            $add_classes($view->display_handler->options['css_class'], [
                'js-media-library-view',
            ]);
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.