Drupal

Changing root folder to /web for Drupal 9/10 in Apache.

When you create a Drupal Project using the composer, it creates two root folders: vendor and web. The web folder contains the developed website, and the vendor folder has dependencies and vendor libraries. When you try to make this website live with the same directory structure, website URL container /web in every URL, which is not a good practice, to remove the /web from the path, you need to tell the Apache server to send all the request to the web folder and web folder must be considered as the root folder for the website.

What is new in Drupal 10

Drupal 10 is finally coming on December 14, 2022, as announced by the Drupal team. This new update is bringing a considerable change in drupal features, looks and underlined technologies. We saw many updates after the Drupal 8 release, but these were either security bug fixes or some minor improvements in which end users could not find the distingue if you did not tell them. But this time, end-users immediately recognise and would love it right after you migrate their website to drupal 10.

Drupal 8 or 9 - Webform - Searching and loading submission using form fields value

In Drupal, sometime we need to deal with already submitted form data. E.g. we may give end user to display some values of submitted records based on provided phone number or any other field.

For this, we would use Drupal database service which would allow us to build select query. In select query , you can mention fields you want to fetch and conditions to filter the data.

And if you fetch submission Id, then you can load the entire submission data.

Here is the code sample.

 

Tags

Drupal 8 - Webform hooks execute before displaying form

Here are some webform hooks which execute before rendering form. These hooks can manipulate form appearance and elements values.


function webform_webform_submission_prepare_form(WebformSubmissionInterface $webform_submission, $operation, FormStateInterface $form_state) {

}


function custom_form_alter(&$form, FormStateInterface $form_state, $form_id) { 
      $form["elements"]["full_name"]["#title"]='New Title';
} 



function custom_preprocess_webform(&$variables) {

}

 

Tags

Drupal 8, Webform - Creating Custom webform handler

Sometime, we need to intercept the values being submitted by website and want to execution our code for these values like sending them to API or submitting them third party URL.

So, webform module gives you freedom to write your code code then may execute at different events of webform submit.

For this, Just create a module and add your code as a plugin.

 

Steps to create custom webform handler:

1 . Create module: create a module named as "custom"

custom.info.yml

Tags

Drupal 8 - Creating Custom Module & Sending value from preprocessor hook to display using custom module

In Drupal 8, most of the work can be done without writing any code but there may be situation where you want to write your logic and then want to send the output of that logic to view.

Here, we are using custom module where we have various hook function available  to write login inside their  body and where you can add your value to final output. Most of the function has parameter &$variables which is the reference of $variables variable to be rendered by drupal to produce final output.

Here is the module structure :

Tags

Drupal - Sending values from preprocessor hook function to view

In Drupal 8, sometime we need to write custom code and output of this custom code to be sent to frontend templates for rendering. Here, we can write the custom code in module but I am writing custom code in *.theme file which has one mytheme_preprocessor function and I want to send a value for rendering in template.

 

Here is the example

function mytheme_preprocess(&$variables) {
  $variables['myname'] = 'Jone Doe';
}

And you can access this value in twig file like given below

{{myname}}

 

Tags

Drupal - Displaying related nodes content into a page like in e-commerce.

There may be requirement when you need to link the contents from different nodes to a specific node. For example, you may want to allow user a facility that they can set the related content from different nodes in a page like we do in ecommerce portal where we configure related products.

This can be achieved in drupal 8 in following steps:

  1. the a field of type content
  2. select the node type you want to link with
  3. select how many related node you want to configure in this newly created field
  4. and save then.

 

Tags

Drupal 7 - Creating block programmatically

Drupal blocks are the reusable content holder that can be placed anywhere in the region of the page. You can manage them and their configuration from the Structure area of the Drupal back-end. Each theme defines certain sections in their page where you can assign the block to them. 

Although, Drupal developer can use the Drupal admin area to manage and create the block, but there is another way to create them as well. You can create the block by doing the coding yourself. This feature of drupal allows you to add the display component to website bases on your programming logic.

Tags