Skip to main content

Posts

Showing posts from 2018

Views Template rendering in Drupal 8

File name suggessations are as follows:  [base template name]--[view machine name]--[view display id].html.twig [base template name]--[view machine name]--[view display type].html.twig [base template name]--[view display type].html.twig [base template name]--[view machine name].html.twig [base template name].html.twig example I used for an "unformatted list" with machine name "view_name" and with a single block, "block_1" views-view-unformatted--view-name--block-1.html.twig Custom Theme template with modules module name = product_view Add .module file /**  * Implements hook_theme().  */ function product_view_theme() {   return [     'product_view' => [       'variables' => [         'product_info' => [],       ],     ],   ]; } In controller return the theme with data:: $aa = array ( 'name' => 'raj' , 'age' => 20 );...

Angular 6/7 basic commands

To set up a new project in angular Download the angular code:  npm install -g @angular/cli install the project ng new project_name ng serve --open --port 42001 ng generate component home ng generate service hero Add Bootstarp 4 in Angular 7  1- npm install bootstrap --save  2- Add css in angular.json file in styles array key ( "node_modules/bootstrap/dist/css/bootstrap.min.css" )  3- Hit ng serve  Download and run Angular Project in local machie:  git clone https://github.com/angular/quickstart.git quickstart npm install (It will download the required modules according to the package.json) npm start or ng serve Live reload not working when change code,  then use sudo ng serve --port 4203 How to use Subscribe in angular when deal with http client with GET/POST menus : Topmenu [] = []; constructor ( private TopmenuService : TopmenuService ) { } ngOnInit () { this . getHeaderMenus (); } getHeaderMenus (...

Setup new git folder

Steps To Configure new folder git setup : git init git config --global user.name "username" git config --global user.email "xxxx@gmail.com" git remote add origin https://github.com/Dharmend/ang-my-dream-app.git git config core.filemode false git pull origin BRANCH_NAME ++++++++++++++++++++++++++++ get single branch checkout directly: git fetch origin BRANCH_NAME git checkout BRANCH_NAME +++++++    Get pull live brnach forcefully (override local files)  ++++++++++++++++ git reset --hard origin/BRANCH_NAME

Debug print drupal 8 variables

Use Devel and kint , and search Kint NOTE: Please make sure devel profile must be kint ksm($form);  // print the form with search option in kint 1 - $form_state->getValues(); // Shows all the form field value 2 -  $form_state->getUserInput(); //to get the user input including buttons clicked +++++++++++++++++++++++ dump(_context|keys) }} // in twig file Compare string in Twig file  {% set form_id = form.custom_data.form_id %} {% if form_id == "node_story_edit_form" %}  Yes i am here  {% endif %}    2 - get the # valued of form in twig templates, : All the array and object value can get by  dot(.) operation while # value get by direct array like  below, if($form['#form_id'] == 'node_video_form' || $form['#form_id'] == 'node_video_edit_form'){        $form['#custom_data'] = array('custom_form_id' => $form['#form_id']);    ksm($form); } in twig file : {{ kint(form['...

How to Create/Apply Patches in drupal git

======== git patch A- Create new branch fix the changes After fix add & commit now create the patch with in the patch directory Now i am at patch branch & compare the changes to master branch command : git format-patch master -o patches patches/ : in this folder create the patch files Examples1 :  git format-patch TARGET_BRANCH  -o PATCH_FOLDER/ Lets suppose we are in patch branch and i do 5 commits to this branch, now to create the patch we will match the code to the traget branch like master in our case, so all the codes which is present in patch branch & not availbale in master branch would be create as patch files, 1 commit for 1 patch. i.e we do 5 commits so 5 commit files will be generated, Here:  -o reprents to store the patch as a directory and specify the director name   Example 2 : Single patch file based on commit id Lets consider the above example, but now i need only one commit changes instead of all, so first check...

Alter entity value before save

How to list the name of Entity types in site to use hooks, use Drupal\Core\Entity\EntityInterface; function hook_entity_presave(EntityInterface $entity) { } or function hook_entity_type_presave(EntityInterface $entity) { } but you should know the exact name of entity type , to do so use : drupal debug:entity (drush command) so for taxonomy case use, function itg_multi_category_entity_insert(Drupal\Core\Entity\EntityInterface $entity) {   if(!empty($entity->bundle()) && $entity->bundle() == 'category_management'){     $entity_values = $entity->toArray();     //Get the custom parent cat id      $parent_custom = $entity_values['field_relation'][0]['target_id'];     if($parent_custom == ''){       $parent_custom = 0;     }     $term_load = \Drupal\taxonomy\Entity\Term::load($entity_values['tid'][0]['value']);     $term_load->set('parent...

dynamic query in drupal 8

QUERY:1  $query = db_select('custom_live_blog','lb');     $query->fields('lb');     $query->condition('lb.blog_nid',$blog_nid);     $query->condition('lb.user_id',$user_id);     $result = $query->execute()->fetchAll();     return $result; Result

Get The field values of node in Drupal 8

use Drupal \ node \ NodeInterface ; /** * Implements hook_ENTITY_TYPE_insert() for node entities. * * This tests saving a node on node insert. * * @see \Drupal\node\Tests\NodeSaveTest::testNodeSaveOnInsert() */ function node_test_node_insert ( NodeInterface $node ) { // Set the node title to the node ID and save. if ( $node - > getTitle ( ) == 'new' ) { $node - > setTitle ( 'Node ' . $node - > id ( ) ) ; $node - > setNewRevision ( FALSE ) ; $node - > save ( ) ; } } Now There is so many functions are there to get the values, For All the functions available visit the API code, https://api.drupal.org/api/drupal/core%21modules%21node%21src%21NodeInterface.php/interface/NodeInterface/8.2.x Some of as below, Node edit form, Drupal 8 Automatically Load the whole object no need to load the entity like below, if ($event->getFormId() == 'node_alexa_audio_clips_edit_form') { $node = \Drupal::ro...

How to theme form in Drupal 8

https://www.bryanbraun.com/2013/08/17/using-hook-form-base-form-id-alter/ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\Element; /* * Implement hook_form_base_id_alter() */ function atadmin_form_node_form_alter(&$form, FormStateInterface $form_state) { $node = $form_state->getFormObject()->getEntity(); if($form['#form_id'] == 'node_story_form' || $form['#form_id'] == 'node_story_edit_form'){ $form['#theme'] = array('at_node_story_form'); }elseif($form['#form_id'] == 'node_video_form' || $form['#form_id'] == 'node_video_edit_form'){ $form['#theme'] = array('at_node_video_form'); }elseif($form['#form_id'] == 'node_photo_gallery_form' || $form['#form_id'] == 'node_photo_gallery_edit_form'){ $form['#theme'] = array('at_node_photo_gallery_form'); }elseif($form['#form_id'] == 'node_po...

How to create drupal 8 multisite in localhost

Drupal has a multisite feature which means, you can run multiple sites with the single code base, but all the sites can have different databases, different configuration & files. For multisite installation on drupal 8, I am going to configure the three sites with single code base atdrupaldev.com btdrupaldev.localhost ithdrupaldev.localhost  Now the question is how to configure it, Let me explain each & every step. Follow the below steps, Step 1: Download the latest Drupal code and put it in your working directory,  for supposing I put the code in var/www/at-drupal-8 Step 2: Create the three databases from php myadmin or any GUI, Step 3: Now open the sites directory and copy the default folder & paste it three times and give names as the site name above, i.e default = atdrupaldev.com default = btdrupaldev.localhost default = ithdrupaldev.localhost So your folder structure would be like below, Step 4: Now provide the databas...

Basic Drush Commands for Drupal 8

Daily Use Drush commands for Drupal 8 Drush is used to perform some admin tasks from CLI , Drush command are different for drupal 7 & Drupal 8 versions, Here I am Providing some most  used drush commands for daily use. If you have more of them that you use regularly in your project, please put them in the comments, as I would love to add them to the list. To use these commands make sure you already installed drush on your system, if still you did't install the drush please install it, by the command sudo apt-get install drush or follow any other article to install it. Drush Command Usage drush cr cache rebuild to drupal drush dl {module_name} or {theme_name} to download the drupal module and themes drush en {module_name} or {theme_name} to enable the downloaded module and themes drush upwd --password="NewPassword" admin update the password of user admin drush sql-cli < ...