Friday, August 16, 2024

Implements hook_css_alter().

 Remove the stable theme css for all anonymous User.

function hook_css_alter(&$css, \Drupal\Core\Asset\AttachedAssetsInterface $assets) {

$current_user = \Drupal::currentUser();

         $roles = $current_user->getRoles();

if(in_array('administrator', $roles)) {

return;

}

    $theme_path = \Drupal::service('extension.list.theme')->getPath('stable');

    foreach ($css as $path => $value) {

        if (strpos($path, $theme_path) === 0) {

            unset($css[$path]);

        }

    }

}

 Remove the stable theme css for all anonymous User on particular node.

function hook_css_alter(&$css, \Drupal\Core\Asset\AttachedAssetsInterface $assets) {

$current_user = \Drupal::currentUser();

    $roles = $current_user->getRoles();

if(in_array('administrator', $roles)) {

return;

}

// Get the path to the 'stable' theme.

$node = \Drupal::routeMatch()->getParameter('node');

if ($node instanceof \Drupal\node\NodeInterface) {

$nid = $node->id();

$node = \Drupal\node\Entity\Node::load($nid);

$template_type = $node->field_select_template->value;

if($template_type == 'course_programswift_template') {

$theme_path = \Drupal::service('extension.list.theme')->getPath('stable');

foreach ($css as $path => $value) {

if (strpos($path, $theme_path) === 0) {

unset($css[$path]);

}

}

unset($css['modules/custom/ms_ajax_form_example/css/ms_ajax_form_example.css']);

unset($css['modules/contrib/social_media_links/css/social_media_links.theme.css']);

unset($css['themes/custom/nexus/assets/css/jquery-ui.min.css']);

}

}

}

No comments:

Post a Comment

If you have any problem please let me know.