Monday 17 September 2018

How to create Schema in custom module drupal 8?

When we need to create our custom module table, So we have to create schema file in custom module root like if our module name is `crudform`, so in the folder of module we will create file which name is `crudform.install`. here our file extension will be `.install` for schema.
In the file we will create function `hook_schema()` with name of `crudform_schema()` and there we will define create structure of table with table name, we have an example for this, Please have a look..

Schema database

function crudform_schema() {
  $schema['crudform'] = array(
    'fields' => array(
          'id'=>array(
            'type'=>'serial',
            'not null' => TRUE,
          ),
          'name'=>array(
            'type' => 'varchar',
            'length' => 40,
            'not null' => TRUE,
          ),
           'email'=>array(
            'type' => 'varchar',
            'length' => 255,
            'not null' => TRUE,
          ),
            'city'=>array(
            'type' => 'varchar',
            'length' => 255,
            'not null' => TRUE,
          ),
            'country'=>array(
            'type' => 'varchar',
            'length' => 255,
            'not null' => TRUE,
          ),
          'message'=>array(
            'type' => 'varchar',
            'length' => 255,
            'not null' => TRUE,
          ),
    ),
    'primary key' => array('id'),
  );
  return $schema;
}

In that function crudform_schema(), our database table name will be `crudform`. here we will define table fields and structure and then return the schema. When we will install our custom module then this table with schema will automatically create in database and with the help of this we can store data in our custom table.


If we want to store default data on table then we have to use `hook_install` function In case of installation of module that data will store automatically. We have an example for this, please have a look..

function crudform_install() {
  $database = \Drupal::database();
  // Add default entry in table.
  $fields = array(
    'name' => 'Sahil',
    'email' => 'sahil@gmail.com',
    'city' => 'Delhi',
    'country' => 'India',
    'message' => 'Hello text',
  );
  $database->insert('crudform')
    ->fields($fields)
    ->execute();
  
  // Add one another entry on this table.
  $fields = array(
    'name' => 'Abhi',
    'email' => 'abhi@gmail.com',
    'city' => 'Delhi',
    'country' => 'India',
    'message' => 'Hello text world',
  );
  $database->insert('crudform')
    ->fields($fields)
    ->execute();
}

There we are creating a $fields array with few data and then store by insert query.
So that's a simple process of 'create schema in drupal 8 custom module'.


Note: If have any suggestions or issue regarding 'How to create Schema in custom module drupal 8?' then you can ask by comments.   

Friday 14 September 2018

What is right query structure for drupal 8?

Here we are defining our drupal 8 query structure with easy mode. You can find/search here INSERT, SELECT, UPDATE, DELETE.
We will explain in easy way with an examples, Please have a look..
That is our table `testing_form` structure..

id candidate_name candidate_mail candidate_number candidate_dob
1 John john@gmail.com 1236547890 2014-10-22
2 Mary mary@gmail.com 5698741230 2014-10-23
3 Karen karen@gmail.com 0123654789 2014-10-23

INSERT Query Structure:
According to php we will define INSERT Query as:

$insert_query = "INSERT INTO testing_form (candidate_name, candidate_mail, candidate_number, candidate_dob) VALUES ('sahil', 'sahil@gmail.com', '9874563210', '22-11-1990')";
mysqli_query($connection, $insert_query);

And now in case of drupal 8, our structure will be.

$fields = array(
    'candidate_name'   => 'sahil',
    'candidate_mail'   => 'sahil@gmail.com',
    'candidate_number' => '9874563210' ,
    'candidate_dob'    => '22-11-1990',           
);
db_insert('testing_form')
    ->fields($fields)
    ->execute();
   
Here 'testing_form' is table name and $fields variable is the data for fields of table.


SELECT Query Structure:
According to php we will define SELECT Query as:

$select_query = "SELECT * from testing_form where id = '2' and id != '3'";
mysqli_query($connection, $select_query);

And now in case of drupal 8, our structure will be.

$result = db_select('testing_form', 't')
    ->fields('t')
    ->condition('id', 2, '=')
    ->condition('id', 3, '!=')
    ->execute()
    ->fetchAll();
   
Here 'testing_form' is table name and our condition is all 2nd id candidate come but not come on 3rd id candidate.

We have some other method for SELECT query with connection in drupal 8..

$connection = \Drupal\Core\Database\Database::getConnection();
$results = $connection->query('select candidate_name, candidate_mail, candidate_number, candidate_dob')->fetchAll();


UPDATE Query Structure:
According to php we will define UPDATE Query as:

$update_query = "UPDATE `testing_form` set candidate_name='karen sood' where id='3'";
mysqli_query($connection, $update_query);

And now in case of drupal 8, our structure will be.

$update_query = \Drupal::database()->update('testing_form')
          ->fields(array('candidate_name' => 'karen sood', 'candidate_number' => '1236547890'))
          ->condition('id', '3')         
          ->execute();
   
Here 'testing_form' is table name and our condition is id number `2nd` of candidate data will update.

 
DELETE Query Structure:
According to php we will define DELETE Query as:

$delete_query = "DELETE from `testing_form` where id='3'";
mysqli_query($connection, $delete_query);

And now in case of drupal 8, our structure will be.

$delete_query = \Drupal::database()->delete('testing_form');
      ->condition('id', '3');   
      ->execute();
   
Here 'testing_form' is table name and our condition is id number `3rd` of candidate data will delete.


So these all are INSERT, SELECT, UPDATE, DELETE queries structure for Drupal 8. Next time we will come with `JOINS`, like how to use `joins` in drupal 8 query with many of conditions.



Note: If have any suggestions or issue regarding 'What is right query structure for drupal 8?' then you can ask by comments.  

Tuesday 11 September 2018

Steps for Add Custom or Add More Language on website.

If we want to add custom language or add multiple languages on our drupal website then we have to add and install `Language Module` first.


Install Language module



Then click on the Configure link or go with this link "/admin/config/regional/language".
Language List

and then you can add custom language by choose last option "Custom Language". After choose you will be redirect in this page.

Choose Custom or other language option
If we are choosing default languages that given by module then automatically will be added on list, but in case of last option custom language we will reach in this page.

Set Custom language code and name

After this you will redirect to listing page of added languages.
Language list
here we can set our default language that will automatically display in language list.

After that we have to check the "Show language selector on create and edit pages" option on content type edit page in "language settings".
Content type edit page for add dropdown option

Then you will be get the language drop-down in body/description type fields on node form.
So this is the simple steps for Add Custom or Add more language in website.



Note: If have any suggestions or issue regarding 'Steps for Add Custom or Add More Language on website.' then you can ask by comments.  

Thursday 6 September 2018

Media entity fields render in drupal 8 Node Twig template.

If we want to load media entity fields on node twig template. It's easy to load media entity, Please have a look..

{# call to media entity fields on node twig #}

{% for key, item in node.field_document_entity_browser %} 
// This is the node field which is connect to media field `field_document_entity_browser`..
{% if node.field_document_entity_browser[key].entity %}
// here we are rendering media entity field which machine name is `field_capt2120`..                           
{{ node.field_document_entity_browser.entity.field_capt2120[key].value }}

// here we are rendering media entity image field which machine name is `field_media_image`..        
{%if item.entity %}
{% set media = item.entity %}
{% set file = media.field_media_image.entity %}
{% set uri = file_url(file.uri.value) %}
                              
<img src="{{ uri }}" alt="{{ media.name.value }}" />
                          
 {% endif %}                           
                                                                                     
 {% endif %}
  {% endfor %}



Note: If have any suggestions or issue regarding 'Media entity fields render in drupal 8 Node Twig template.' then you can ask by comments.  

Wednesday 5 September 2018

Paragraph fields render in drupal 8 Node Twig template.

If we want to load paragraph entity fields on node twig template. It's easy to load paragraph entity, Please have a look..

{# call to paragraph entity fields on node twig #}

{{ node.field_professional_features.entity.field_professional_services_name.value }}
 // here `field_professional_features` is the content type field and `field_professional_services_name` is the related paragraph field..

{{ node.field_professional_features.entity.field_professional_services_summ.value }}
  // here `field_professional_features` is the content type field and `field_professional_services_summ` is the related paragraph field..

// here we are calling paragraph image field..

{% for key, image in node.field_professional_features %}
// here `field_professional_features` is the content type field..
{%if image.entity %}
{% set paragraph = image.entity %}
{% set file = paragraph.field_professional_services_imag.entity %}
 // here `field_professional_services_imag` is the related paragraph field..
{% set uri = file_url(file.uri.value) %}

<img src="{{ uri }}" alt="{{ paragraph.name.value }}" />

{% endif %}
{% endfor %} 



Note: If have any suggestions or issue regarding 'Paragraph fields render in drupal 8 Twig template.' then you can ask by comments.  

Tuesday 4 September 2018

Replace any string or value in twig template variable.

If we want to Replace the value in twig template then we have an example and syntax, please have a look..
{#
    ****  replace '-' with '$' sign in vidyard ID ****  
#}

{% set twig_content_variable  = node.field_resources_vidyard_id.value  %}   // here we are applying on field_resources_vidyard_id field..
{% set replace_value_var      = '-' %}
{% set replace_with_value_var = '$' %}
{%if twig_content_variable %}
   {% set vidyard_function_id = twig_content_variable|replace({ '-': '$' }) %}
{% endif %}


Note: If have any suggestions or issue regarding 'Replace any string or value in twig template variable.' then you can ask by comments. 

Programmatically Media load in drupal 8.

If we want to load Media entity. we have to add library on the top of page. ( use Drupal\media\Entity\Media; )
Then its easy to load media entity by the syntax.
   
Syntax:
$media_detail = Media::load($mid);    // here $mid is media entity id       
   
now if we want to get image field data of media field then syntax is..

$media_img = file_create_url($media_detail->field_media_image->entity->getFileUri());
 // here field_media_image is field name in media entity
   
now if we want to get any text field value then syntax will be..

$media_caption = $media_detail->get('field_capt2120')->getValue();   
// here field_capt2120 is the machine name of caption field in media entity    

foreach($m_caption as $mkey){
   $capval = $mkey['value'];    // here we will get the caption value           
}


Note: If have any suggestions or issue regarding 'Programmatically Media load 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...