Tuesday, July 21, 2020


Form API conditional field required and visible in Drupal 8

A few months ago, I was working with a Drupal shop and was surprised that they didn’t know how to make a field conditional and required using the Form API. I have decided to publish my findings here:

I am going to give a code example on how to do it here:

   $form['fruit'] = [
      '#type' => 'select',
      '#title' => 'Fruit Types',
      '#options' => [                                                                                                              
           'citrus' => $this->t('Citrus'),                                                                                      
            'other' => $this->t('Other'),                                                                                     
         ],                                                                                                                   
      '#attributes' => [
        'name' => 'fruit',
      ],
    ];
    $form['citrus_fruits'] = [
      '#type' => 'textfield',                                                                                              
      '#title' => 'Type in your fruit',                                                                                    
      '#size' => '60',
      '#states' => [                                                                                                         
        'required' => [
          ':input[name="fruit"]' => ['value' => 'other'],
        ],                                                                                                                   
        'visible' => [
          ':input[name="fruit"]' => ['value' => 'other'],                                                                    
        ],
      ],
    ];

With this FormAPI code, you can see that it is possible to make a field required and visible based of a choice of another field. you can add other properties too, like disabled and such.

Wednesday, August 21, 2019

node_load loads a node by id in drupal 8

node_load loads a node by id in drupal 8

access taxonomy fields programatically in drupal 8

Access one term:    
    $term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load(TID);
    $value = $term->FIELD_MACHINENAME->value;

Example : load title and language of term id =2

    $term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load(2);
    $title = $term->name->value;
    langcode = $term->langcode->value;

And for load Multiple

  $terms = $entityManager->getStorage('taxonomy_term')->loadMultiple($t‌​erms);
  foreach ($terms as $term) {
      var_dump($term->tid->value); //return tid of term
   var_dump($term->name->value); //return title of term
  }

Tuesday, June 25, 2019

Migration path info for drupal 8

if the path of your csv file is in the root of your project then:
source:

  plugin: CSV

  path: your_file.csv
the best practice is to put the csv file into your migrate module in a directory "artifacts" for example the structure should be as below:
-- custom_migrate_module

   +-- artifacts

         +-- your_file.csv

   +-- config

   +-- src

   +-- custom_migrate_module.info.yml
in this case your migration confg file should be:
source:

  plugin: CSV

  path: modules/custom/custom_migrate_module/artifacts/your_file.csv

Thursday, June 13, 2019

Useful Drupal 8 modules

1. Conditional Fields - Allows for easy creation of conditional fields for content types.

Tuesday, June 11, 2019

find unprintable characters in vim

Using range in a [] character class in your search, you ought to be able to exclude the ASCII hexadecimal character range, therefore highlighting (assuming you have hlsearch enabled) all other characters lying outside the ASCII range:

/[^\x00-\x7F]
This will do a negative match (via [^]) for characters between ASCII 0x00 and ASCII 0x7F (0-127), and appears to work in my simple test. For extended ASCII, of course, extend the range up to \xFF instead of \x7F using /[^\x00-\xFF].

You may also express it in decimal via \d:

/[^\d0-\d127]
If you need something more specific, like exclusion of non-printable characters, you will need to add those ranges into the character class [].