Showing posts with label Drupal-8. Show all posts
Showing posts with label Drupal-8. Show all posts

Tuesday, 17 May 2022

Drupal 8 programmatically get list of themes installed

 To get list of themes on a Drupal site programmatically, please use following code:


  $modulesList = \Drupal::service('extension.list.theme')->reset()->getList();
  if (! empty($modulesList)) {
    echo "<strong>Themes List:</strong>";
    echo "<br/>";
    foreach ($modulesList as $moduleName => $moduleObj) {
      echo "<br/>";
      echo '<strong>Machine Name: </strong>' . $moduleName;
      echo "<br/>";
      echo '<strong>Humand Readable Name: </strong>' . $moduleObj->info['name'];
      echo "<br/>";
      echo '<strong>Description: </strong>' . $moduleObj->info['description'];
      echo "<br/>";
      echo '<strong>Package: </strong>' . $moduleObj->info['package'];
      echo "<br/>";
      echo "-----------------------------------------------------------------";
    }
  }


Output:


Themes List:

Machine Name: bartik
Humand Readable Name: Bartik
Description: A flexible, recolorable theme with many regions and a responsive, mobile-first layout.
Package: Core
-----------------------------------------------------------------
Machine Name: claro
Humand Readable Name: Claro
Description: A clean, accessible, and powerful Drupal administration theme.
Package: Core
-----------------------------------------------------------------
Machine Name: classy
Humand Readable Name: Classy
Description: A base theme with sensible default CSS classes added. Learn how to use Classy as a base theme in the Drupal 8 Theming Guide.
Package: Core
-----------------------------------------------------------------
Machine Name: seven
Humand Readable Name: Seven
Description: The default administration theme for Drupal 8 was designed with clean lines, simple blocks, and sans-serif font to emphasize the tools and tasks at hand.
Package: Core
-----------------------------------------------------------------
Machine Name: stable
Humand Readable Name: Stable
Description: A default base theme using Drupal 8.0.0's core markup and CSS.
Package: Core
-----------------------------------------------------------------

Wednesday, 17 March 2021

Drupal 8 Blocks: Visibility Hide/Show for certain Roles.

 In Drupal 8, while showing any block, there are visibility settings where, we can select which pages the block can be visible.

The same form has an underlying functionality to hide the block for the selected list or URLs.

Unfortunately, the same is not the case of Roles.

You can select which roles you want to display the block, but, you cannot select which Roles you want to hide the block.

Following code adds a Negate condition to Roles Visibility:


/* Negate the condition to decide visibility of which user roles to show/hide. */

function YOUR_MODULE_form_block_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

$form['visibility']['user_role']['negate'] = [

'#type' => 'radios',

                '#title' => '',

'#default_value' => 0,

'#options' => [

t('Show for selected Roles'),

t('Hide for selected Roles'),

],

];

}

This adds two radio buttons under the form: Roles.

Following is the screen shot for the same:



Tuesday, 19 March 2019

Drupal 8 uninstall a module programmatically

Drupal 8 does not allow to enable/disable modules like previous versions.

You have to completely uninstall a module to get rid of it.

You can install the module be:

<?php
  
\Drupal::service('module_installer')->install(['admin_toolbar']);?>



You can uninstall the module by:

<?php
  
\Drupal::service('module_installer')->uninstall(['admin_toolbar']);?>

Thursday, 28 February 2019

How to add a class to body Drupal adding a class to “body”

You can use hook_preprocess_html() in your theme's yourtheme.theme file.

Following code will add a body class bdyCls to <body> if certain condition matches.

/**
 * Implement hook_preprocess_html
 */
function yourtheme_preprocess_html(&$html) {
 if ($condition) {
  $html['body_class'] .= 'your-custom-css-class';
 }
}

Wednesday, 27 February 2019

Drupal 8 login a user programatically

Question: How to login a user programmatically in Drupal 8?

Steps:

In your module, create a menu call back

example.externallogin:
  path: '/external/login/{token}'
  defaults:
    _controller: '\Drupal\example\Controller\ExamplesController::externallogin'
    _title: 'Offers'
  requirements:
    _access: 'TRUE'


in your controller,

modules/example/src/Controller/ExamplesController.php,

use Drupal\user\Entity\User;
use Symfony\Component\HttpFoundation\RedirectResponse;

public function externallogin($token = NULL) {
$uid = 111; // Can be any valid Drupal's user id.
if(isset($uid)) {
  $user = User::load($uid);
  user_login_finalize($user);
}
return ['#type' => 'markup', '#markup' => $this->t('You are logged in')];
}

And user is logged into Drupal.

Monday, 25 February 2019

Drupal 8 prevent caching of programmatically created block.

I was working on creating a custom block programmatically.

The requirement was to show the file uploaded to a specific node.

The problem was block was caching and showing same value for each node page.

After a lot of Google search for module hooks, theme hooks, I found that there is an inbuilt function in the block definition itself.

getCacheMaxAge()

This function can set cache age to 0 hence disabling to cache the block.

Final code:

<?php
/**
 * @file
 * Contains \Drupal\sample\Plugin\Block\XaiBlock.
 */
namespace Drupal\sample\Plugin\Block;
use Drupal\Core\Block\BlockBase;
//use GuzzleHttp\json_decode;
/**
 * Provides a 'article' block.
 *
 * @Block(
 *   id = "article_block",
 *   admin_label = @Translation("Article"),
 *   category = @Translation("Custom article block sample")
 * )
 */
class ArticleBlock extends BlockBase {
  /**
   * {@inheritdoc}
   */
public function build() {
$nid = NULL;
$content = NULL;
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
// You can get nid and anything else you need from the node object.
$nid = $node->id();
}
if (! empty($nid)) {
$node = \Drupal\node\Entity\Node::load($nid);
$changedVal = $node->get('changed')->getValue();
$changedVal = ! empty($changedVal[0]['value']) ? $changedVal[0]['value'] : '';
if ($changedVal) {
$changedVal = date('d F Y', $changedVal);
}
$content .= '<p class="txt">Last updated ' . $changedVal . '</p>';
}
return array(
'#type' => 'markup',
'#markup' => $content
);
}
/**
     * {@inheritdoc}
     */
    public function getCacheMaxAge() { // <- This function disables block caching.
return 0;
    }
}

Parenting tips to inculcate learning habits in your kid

Parenting tips to inculcate learning habits in your kid Tip #1) Children do not learn things, they emitate. So, try to do things by yours...