logo

Blog

27 July

How render term field on node template in drupal 9

Published On: Thu, 07/27/2023 - 16:56

In Drupal 9, you can render a term field on a node template using the field_view_field() function. This function takes the following parameters:

Entity type: The type of the entity (in this case, it's "node"). Entity: The node object that contains the term field. Field name: The machine name of the term field you want to render. Display mode: The view mode you want to use to render the field (e.g., "full", "teaser", "default", etc.).

Here's an example of how to render a term…

23 June

To set a default value for the username field in a user login form using a form alter hook in Drupal 8, you can use the following code:

/**
 * Implements hook_form_alter().
 */
function yourmodule_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id === 'user_login_form') {
    // Set the default value for the username field.
    $form['name']['#default_value'] = 'your_default_username';
  }
}

Replace 'yourmodule' with the…

1 June

What is the purpose of Ctools in Drupal 9

Published On: Thu, 06/01/2023 - 17:53

In Drupal 9, Ctools (Chaos tools) is a suite of developer tools and APIs that provide various functionalities to enhance and simplify module development. Ctools is not specific to Drupal 9 and has been widely used in previous versions as well.

The primary purpose of Ctools is to provide a set of reusable utilities and APIs that help developers build complex and interactive features in Drupal. Some of the key features and purposes of Ctools in Drupal 9 include:

Plugin…

1 June

What are the benefits of using Drupal

Published On: Thu, 06/01/2023 - 17:18

Drupal is a powerful and flexible open-source content management system (CMS) that offers numerous benefits for website development and management. Here are some of the key benefits of using Drupal:

Flexibility: Drupal provides a highly flexible and extensible framework that allows developers to build websites and applications tailored to specific needs. It offers a wide range of modules, themes, and APIs, enabling the creation of custom functionalities and designs.

1 June

Show Related Content by Category Using Views Drupal 8 or 9

Published On: Thu, 06/01/2023 - 11:20

Follow these step to make block using views to show the related content according to category of the current content page.

Edit the views and open Advance section

Add the  'Content: Has taxonomy term ID' in CONTEXTUAL FILTERS. Check 'Provide default value' option and then select the 'Taxonomy term ID from URL'.

 

3.     Check the 'Load default filter from node page, that's good for related taxonomy blocks' option.

4.     Check the 'Limit terms by…

24 March

How can run my code on user login Drupal 8

Published On: Fri, 03/24/2023 - 15:50

In Drupal 8, you can execute code before user login by implementing a custom module and using a hook to execute the code. Here are the steps you can follow:

Create a custom module by creating a directory in the modules directory of your Drupal installation. For example, if you want to name your module "custom_login", create a directory named "custom_login" in the modules directory.

Create a custom_login.info.yml file in your module directory with the following…

20 March

Custom code to save value in error log in Drupal 9

Published On: Mon, 03/20/2023 - 15:40

To save values in the error log in Drupal 8, you can use the following custom code:

\Drupal::logger('my_module')->error('The error message: @variable', [
  '@variable' => $value_to_save,
]);

In the code above, replace my_module with the name of your module, and replace The error message with the message you want to include in the log entry. You can include any number of placeholders in the message using the @variable syntax.

The $value_to_save variable…

14 March

In views template how can check user is logged in drupal 8

Published On: Tue, 03/14/2023 - 18:20

In a Drupal 8 views template, you can check if a user is logged in by using the following code

{% if logged_in %}
  Welcome, {{ user.name }}
{% else %}
  {{ path('user.login') }}
{% endif %}

Explanation:

    The logged_in variable is a boolean that is set to true if the user is logged in, and false if they are not.     The user variable contains information about the currently logged-in user, such as their username (user.name),…

14 March

Using custom code disable cache from menu block in drupal 8

Published On: Tue, 03/14/2023 - 14:47

To disable caching for a specific menu block in Drupal 8 using custom code, you can implement the hook_block_view_alter hook in your custom module or theme. Here's an example implementation:

/**
 * Implements hook_block_view_alter().
 */
function mymodule_block_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) {
  if ($block->getPluginId() === 'system_menu_block:my-menu') {
    // Disable caching for the menu block.
    $build['#cache']['max-age…
				  

15 February

CUstom code if else condition in twig template drupal 8

Published On: Wed, 02/15/2023 - 20:55

In Drupal 8, you can use the {% if %} statement to create if-else conditions in Twig templates. Here's an example:

{% if condition %}
  // Code to execute if the condition is true.
{% else %}
  // Code to execute if the condition is false.
{% endif %}

Replace "condition" with your desired condition to evaluate. You can use logical operators such as and, or, and not to create more complex conditions.

Here's an example of how you might use this in a Drupal 8 Twig…