Latest Articles

Our latest news updates and insightful blog posts, designed to empower you with the knowledge and strategies you need to succeed.

Disable cache for specific page routing in custom module in Drupal 8 or 9

Example is mentioned below where I have created a path (/mycustompath) so I want disable cache for this page then I have add following syntax with my code in the file (mymodule.routing.yml) or in your routing yml file

 options:
    no_cache: 'TRUE' 

Complete example as below

mymodule.form:
 path: '/mycustompath'
 defaults:
   _title: ''
   _form: '\Drupal\workassign\Form\MyTestForm'
 requirements:
   _permission: 'mymodule custom'
 options:
    no_cache: 'TRUE'…

Custom code to save value in error log in Drupal 9

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…

Custom code to disable or hide Node Attribute or Tabs in Drupal 8 or 9

When you create or edit a node there is some default option or attribute in Node Form so we can disable or hide through custom code by using following code in our custom module.

Node Form Attribute or Tabs in Drupal 8

In my example trying to to through folrm_alter hook. Following code using hide URL Alia…

Custom code to add meta tag in Drupal 7

In your theme's template.php file, you could add something like **/

/** * Implements hook_preprocess_html */ 
function THEME_NAME_preprocess_html(&$vars) { 
	if (current_path() == 'my/custom/path') { 
		$description = array( 
		'#type' => 'html_tag', 
		'#tag' => 'meta', 
		'#attributes' => array( 'name' => 'description', 'content' => 'here all description goes', )
		 ); 
		 
		 drupal_add_html_head($description, 'description'); 
	} 
 }
 

Custom code on comment publish in Drupal 8

Custom code on comment publish in Drupal 8

If you want to add your custom code on published of comment then we can use hook"hook_comment_update"

"$comment->isPublished()" will check comment in published

For example you want to send mail when comment published or unpublished so you can follow these step

Open your .module file and add the following code, for example your module name is "mymodule" then

 

function mymodule_comment_update($comment) {…

Custom code if else condition in twig template drupal 8

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 o…

Crreate user programatically in Drupal 8

Using following step and method to create user programatically

Must be use this line in above the code

use Drupal\user\Entity\User;

I have added two extra fields in user account that is mentioned below.

Phone - This is text type field. field_phone (Machine name)

Subscribe - This is multiple select type field has machine name as field_subscribe. Following options in

Colorbox image gallery not working on Mobile in Drupal

Step 1 - Go to configuration

Step 2 - Open Colorbox settings


Step 3 - Click on Advance setting


There is an option of Mobile detection if its "On" now it should be "Off"

Now clear cache I hope its work now.

There is setting screenshot image

Code to create custom form in Drupal 8 and 9

Here is an example of a custom form with an email and message field in Drupal 8 or 9

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Class CustomForm.
 */
class CustomForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'custom_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['email'] = [
      '#type' => 'email',
      '#title…

CKEditor automatically strips classes from div in Drupal


Using CKEditor as a back end editor on my website. It is driving me round the bend though as it seems to want to change the code to how it sees fit whenever I press the source button.

Soln 1.
Follow this given step to resolve this issue.
Go to "Admin >> Configuration >> CKEditor"; under Profiles, choose your profile (e.g. Full).
Edit that profile, and on "Advanced Options >> Custom JavaScript configuration" add config.allowedContent = tru…

Cheat Sheet for using Twig Tweak in Drupal 10

1. **Render a View**:

- Use `drupal_view('view_name', 'display_id')` to embed a view.

- Replace `'view_name'` with the actual view name and `'display_id'` with the desired display (default is `'default'`).

- Example: `{{ drupal_view('who_s_new', 'block_1') }}.

2. **Get the Result of a View**:

- Retrieve the result of a view using `drupal_view_result('view_name', 'display_id')`.

- Parameters are the same as for rendering a view.

- Example: `{{ dr…

Can't save or submit anything after enable Boost in Drupal 7

I got the problem. After enabled and configured Boost, I cant change any single setting on the site, nothing gets saved. When hitting "Save" button on node edit form or any other submit form, the page just gets reloaded submit not working.

Solution.

This problem occurs only in the dev-version or Version 1.1. Version 1.0 is working well.

1. First install version 1.0 of the module. Activate that module. Go to admin/config/system/boost/htaccess/generator and ta…