Friday 6 August 2021

How to resolve max execution time error in drupal ?

When you found error regarding 'max_execution_time' exceed, then you can follow steps for resolve this error:


Steps: 

You can put the below given code in drupal 8 index.php file.

ini_set('max_execution_time', 120); //for 120 seconds

ini_set('max_execution_time', 0); //for infinite process

OR

You can add below given code in settings.php file. (sites/default/settings.php)

ini_set('max_execution_time', 120); //for 120 seconds

ini_set('max_execution_time', 0); //for infinite process


Both will work for resolve that error.


Note: If have any suggestions or issue regarding 'How to resolve max execution time error in drupal ?' then you can ask by comments. 

 

Friday 30 July 2021

How to rebase and merge in master or other branch in git?

Simple way to rebase and merge the branch into other branch/master in git is going to explain by some steps:

1. Go to your Branch. // which you want to merge... in Git bash

2. git rebase master_branch_name // underline word will be your master or other branch where you want to upload.

This rebase will push your commits to top of the master/other branch..

3. Then pull and push the code by git desktop software.

4. Then go to the master or head branch (git checkout branch name)

5. git merge your_branchname  // underline word will be your lower branch name.


Note: If have any suggestions or issue regarding 'How to rebase and merge in master or other branch in git?' then you can ask by comments. 

How to point the branch changes to Acquia by Git?

Simple way to push the branch changes to Acquia Box(environment):
1. Go to the Acquia account click on box(there you want to point the branch changes) and follow the image instructions:


afterthat it will automatically sync your files related changes to box.

2. Go to the project folder or docroot and open cmd terminal and run the command.
drush sa  // for print all project aliases or environments/boxes of acquia
Then
Take the alias of box where you want to point the config changes..
drush @alias cim  //for point config changes

3 If you want to create the reset password for new or already exist box link:
drush @alias uli  //for reset password link

Note: If have any suggestions or issue regarding 'How to point the branch changes to Acquia by Git?' then you can ask by comments. 

Thursday 8 July 2021

How to convert image in webp format programmatically?

We are giving an example for convert normal image to webp image format programmatically. Here all code/examples are already used in our projects.

Example:

use Drupal\image\Entity\ImageStyle;
use Drupal\media\Entity\Media;
use Drupal\responsive_background_image\ResponsiveBackgroundImage;

if ($tab_img = $tab_entity->get('field_image')->getValue()) {

          $tab_img = array_shift($tab_img);

          $mid = $tab_img['target_id'];

          $media = Media::load($mid);

          $fid = $media->field_image->target_id; 

          // if you want simple image url..

          $style = 'max_160w';  //pass image format

          $url = content_asset_image_url($fid, $style);

          $variables['tab_titles'][$tab['target_id']]['icon'] = $url;

          // if you want image convert into webp image format..

          $file = File::load($fid);

          $uri = $file->getFileUri();

          $variables['tab_titles'][$tab['target_id']]['icon']  = [

            '#theme' => 'responsive_image',

            '#responsive_image_style_id' => 'responsive_asset_300x300',  //pass responsive image format

            '#alt' => 'Resource Content Asset',

            '#uri' => $uri,

            // Add `#attributes` key with alt text.

            '#attributes' => [

              'data-lazy'=> TRUE,

            ],

          ];

        }

 // Content Asset function to get Image URL

function content_asset_image_url($file_id, $style_choice) {

  $file = File::load($file_id);

  $image_uri = $file->getFileUri();

  $style = ImageStyle::load($style_choice);

  $url = $style->buildUrl($image_uri);

  return $url;

}


Note: If have any suggestions or issue regarding 'How to convert image in webp format programmatically?' then you can ask by comments. 

Friday 18 June 2021

How to add, commit, push files and database changes in git process?

We are going to explain not only famous git commands but also will discuss very useful Drush command for upload config(database) changes on a branch.

Follow these steps as given for upload files and database(config) changes on the branch.

1. drush cex => This command is used to upload a database(config) changes on the branch.

2. git status => To check the changes in files and database(config).  

3. git add => This command is used to add your changes on a branch, here you have to git add \<changes file path or config changes path>

OR

3. git add --all => This command is used to add all edited file changes at the same time.

4. git commit => This command is used to commit the added changes on branch by- [git commit -m "type message for commit here"] 

5. git push => This command is used to push all commit changes on the current branch.


OR 


If git operations are difficult for you then follow these given steps.

1. drush cex => This command is required first for upload database(config) changes on the branch.

2. Then use "Git desktop" software for all above git operations on the branch by great GUI. 

download link: https://desktop.github.com/


Note: If have any suggestions or issue regarding 'How to add, commit, push files and database changes in git process?' then you can ask by comments. 

How to show only used taxonomy terms or category in view exposed filters?

In Drupal 8, we have a contributed module that name is "Selective Better Exposed Filters". That is to show only used taxonomy terms or categories in view exposed filters.

https://www.drupal.org/project/selective_better_exposed_filters

But it will depend on the better_exposed_filters module.

https://www.drupal.org/project/better_exposed_filters

better-exposed filters (selective) module will enhance the exposed form style. you have to enable it in exposed form, check the box "show only used terms" at the bottom. 

If you have multiple exposed filters then you will have to enable them individually under the "more options for".


Note: If have any suggestions or issue regarding 'How to show only used taxonomy terms or category in view exposed filters?' then you can ask by comments. 

Thursday 17 June 2021

How to render nested paragraph field in drupal 8 paragraph twig?

It's a very easy way to render nested paragraph fields, we are going to explain with 2 methods for link field data.

Method 1: Suppose your paragraph(entity reference revisions) name is 'simple_card', so your file name will be "paragraph--simple-card.html.twig" and we have to render inner paragraph(entity reference revisions) link field data, so our approach will be...

{% for item in content.field_p_link['#items'] %}

{{ item.entity.field_link.uri }}

{% endfor %}


Method 2: Our second method will be direct one line code for this inner 'field_link' field data is...

{{ content.field_p_link['#items'].entity.field_link.uri }} 


// here field_p_link is the first paragraph revision field and field_link is a nested paragraph field.

Imp: Other text fields simply render by 

          {{ content.field_p_link['#items'].entity.field_first_name.value}} 


Note: If have any suggestions or issue regarding 'How to render nested paragraph field in drupal 8 paragraph twig?' then you can ask by comments. 

Monday 14 June 2021

Create and move to New branch in Git.

 We are going to explain with some steps..

Git Bash process..

1. 'git checkout' // from current branch.

2. 'git checkout postlaunch_qa'  //You can rename underline area, from where you get the branch data.

3. git pull

Cmd process..

  • mysql -u drupal -p cvent < dev-cvent.sql // Run the fresh database here. (dev-cvent.sql this will be on root)  [optional - not every time required]

  • drush cr  //for clear cache

  • drush cim  //for config import changes.

4. 'git branch --all'  // for get all branches. (optional)

5. 'git branch <new branch name>' // for create new branch for new work.

6. 'git checkout <new branch name>' //for checkout that branch

After this process For login/reset password:

drush uli  // run this command and will return  kind of url:

'/en/cms/reset/1/1627312926/saMxys3WxEQ2I8DowhnRIhEYbxNdkc3yVuhwj9DQ33c/login'

put this on url website url and it will redirect to reset password for login.

Note: If have any suggestions or issue regarding 'Create and move to New branch in Git.' then you can ask by comments. 

Tuesday 8 June 2021

How to generate csv file from table data and save in specific path?

 A simple way to generate a CSV file from a database table is the "file_put_contents" function. here we are giving an example of this.

Code:

// Given exmaple is relate from drupal query..

function array2csv($data, $delimiter = ',', $enclosure = '"', $escape_char = "\\")

{

    $f = fopen('php://memory', 'r+');

    foreach ($data as $item) {

        fputcsv($f, $item, $delimiter, $enclosure, $escape_char);

    }

    rewind($f);

    return stream_get_contents($f);

}


$data = array();  
$watchdog_results = db_query("SELECT wid,uid,type,message,severity,link,location,referer,hostname,timestamp FROM watchdog")->fetchAll();

if(!empty($watchdog_results)){

// for adding first Header row

$data[] = array(

  'wid',

  'uid',

  'type',

  'message',

  'severity',

  'link',

  'location',

  'referer',

  'hostname',

  'timestamp',   

);

// for adding content rows

foreach($watchdog_results as $line){

   $data[] = $line;  

}

}

$array = json_decode(json_encode($data), true);

$csdata = array2csv($array);

file_put_contents('/code/watchdog-report.csv', $csdata);

// here "/code/watchdogcustom-report.csv" is our located file path and filename, you can write filename as you want and also locate in any path.

In Drupal, you can use this code anywhere you want like, in any controller or hook_cron function, etc.


Note: If have any suggestions or issue regarding 'How to generate csv file from table data and save in specific path?' then you can ask by comments. 

Friday 30 April 2021

How can we update Drupal version?

Easy way to update Drupal website, we just have to follow some steps.

Step 1: Please take website full backup(files + database). We can use Backup and Migrate module ( https://www.drupal.org/project/backup_migrate )

Step 2: Site should be in maintanance mode because it can be broken updation time.

Step 3: Please take care of your patch code that already applied in core.

Step 4: Take a backup of Important files like 'robots.txt', '.htaccess', 'settings.php', 'web.config'. We will not update these files otherwise you will not get your old website.

Step 5: Now upload or override New version of drupal files except of given Step 4 files and 'modules','profiles','themes' folder because in new version these 3 folders will be empty.

Step 6: After full upload of files override Step 4 files.

Step 7: Clear the cache of website.

Step 8: Run update.php file via https://yourserver.com/update.php But by default this file is blocked, you have to enable this feature by setting.php file, go to setting.php file and change the option $settings['update_free_access'] = FALSE; to $settings['update_free_access'] = TRUE;

Step 9: After that save setting.php file and clear cache and hit https://yourserver.com/update.php after completion of process you can check you finally updation of site via https://yourserver.com/admin/reports/updates.


Note: If have any suggestions or issue regarding 'How can we update Drupal version?' then you can ask by comments. 

What is the difference between upgrade and update in Drupal?

It's easy to understand, what is upgrade and update, please have a look.

Update: If Drupal gives minor change version like Drupal 8.0.1 to 8.2.0, so that is update.

Upgrade: If Drupal gives major changes version like Drupal 6 to Drupal 8 or from Drupal 7 to Drupal 8,  that is upgrade.


Note: If have any suggestions or issue regarding 'What is the difference between upgrade and update in Drupal?' then you can ask by comments. 


Thursday 29 April 2021

How to load CSS or JS in footer?

If, we have to load CSS and JS in header or footer in Drupal 8, so we will call our CSS or JS in '*.libraries.yml' file, there we will use scope attribute for this purpose.

we just have to pass the option value either header or footer.

like: scope:footer  or  scope:header 

Example:

If our yml file is mytheme.libraries.yml and we are attached assets there..

slick_slider:

  css:

    theme:

      slick.min.css: { type: external, scope:footer }

  js:

    slick.min.js: { type: external, minified: true, scope: header }

  dependencies:

    - core/jquery


Note: If have any suggestions or issue regarding 'How to load CSS or JS in footer?' then you can ask by comments. 


Monday 19 April 2021

How to remove or cleanup old aggregated css and js files?

It's very problematic question that how we remove old aggregated css and js files otherwise folder will be too large.

In drupal we have 2 functions drupal_clear_css_cache() and drupal_clear_js_cache() that are creating our aggregation files and store in css and js folder in files/css or files/js folder.

In drupal we have on special variable `drupal_stale_file_threshold` and the default value of this 30 days, it means after 30 days old aggregated file will automatically removed and we can change this 30 days value or less then 30 days like..

Code:

variable_get('drupal_stale_file_threshold', 2592000) //for 30 day. 

variable_set('drupal_stale_file_threshold', 172800) //for 2 days.  


Note: In drupal, we have advance aggregation module, there we have delete old aggregated files option.



Note: If have any suggestions or issue regarding 'How to remove or cleanup old aggregated css and js files?' then you can ask by comments. 

How to permanent delete unused files in Drupal 8?

Drupal 8, we can programmatically delete our unused files(assets) easily. we just have to write the code in custom module file in `hook_cron` function. After put the code and clear the cache whenever you hit the cron manually or automatically will run this code will execute.

Code:

/**

 * override hook_cron()

 */

function mymodule_cron()  // mymodule will replace with module name

{

  // get all files ids

  $fids = Drupal::entityQuery('file')->execute();

  $file_usage = Drupal::service('file.usage');

  // loop all fids and load files by fid

  foreach ($fids as $fid) {

    $file = Drupal\file\Entity\File::load($fid);

    $usage = $file_usage->listUsage($file);

    // check if file not used

    if (count($usage) == 0) {

      $file->delete();

    }

  }

}


Note: If have any suggestions or issue regarding 'How to permanent delete unused files in Drupal 8?' then you can ask by comments. 

How to resolve max execution time error in drupal ?

When you found error regarding 'max_execution_time' exceed, then you can follow steps for resolve this error: Steps:   You can put t...