Saturday, May 25, 2019

Create node programatically with translation content in Drupal 8

            $node = Node::create(array(
                'type' => 'news',
                'langcode' => 'en',
                'uid' => '1',
                'status' => 1,
                'title' => $node['Title'],
                'field_description' =>array(0=>array('value'=>$node['PublishingPageContent'],'summary'=>$node['Comments'],'format'=>'full_html')),
                'field_news_type' =>121
         ));

             $node->save();
             $node_fr = $node->addTranslation('fr');
             $node_fr->title = 'frenchtitle';
             $node_fr->body->summary = 'summary';
             $node_fr->body->value = 'frenchcontent';
             $node_fr->body->format = 'full_html';

            //Saving the node
             $node_fr->save();   

Create node programtically with image content in Drupal 8

              $file_image = '/apps/sites/test.jpg';
                $file_content = file_get_contents($file_image);
                $directory = 'public://Migimage/';
                file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
           
                $file_image = file_save_data($file_content, $directory.basename($src),  FILE_EXISTS_REPLACE);
                $fid = $file_image->id();


    $node = Node::create(array(
                'type' => 'news',
                'langcode' => 'en',
                'uid' => '1',
                'status' => 1,
                'title' => $node['Title'],
                'body' =>array(0=>array('value'=>$node['valuecontent'],'summary'=>$node['summary'],'format'=>'full_html')),
                'field_test_type' => '233',
                'field_image' => array(
                                  'target_id' => $fid,
                                  'alt' => 'Sample',
                                  'title' => 'Sample File'
                                  )
             ));

Tuesday, March 17, 2015

Delete a field programatically from a content type



<?php

field_delete_field($field_name);
field_purge_batch();

// note: only run this if you need the field deleted immediately!! otherwise it will just be deleted the next time the cron runs

?>

If you manually delete fields from the database, many things can go wrong. For example, hook_field_delete will not be called and one of your modules might fail and crash the site.

Thursday, December 11, 2014

How to get alias path in drupal

You can get aliased path using drupal_get_path_alias API Function..
$alias_path = drupal_get_path_alias();
$alias_arguments =  explode('/', $alias_path);

print $alias_arguments[0];

Tuesday, October 28, 2014

Save a file in a server path in PHP

<?php

$urlfile =  "c:/text.xls";
$my_excel_content = file_get_contents($urlfile);
$my_excel_filename = "/root/Report-".md5(session_id().microtime(TRUE)).".csv";
file_put_contents($my_excel_filename,$my_excel_content);

?>

Thursday, February 27, 2014

Alter single select box or multiselect box to radio buttons/checkboxes in exposed filters views

You can download "Better Exposed Filters" module to convert single select or multi select boxes to radio button or check boxes.

1. Go to "Advanced" section in views.

2. Check for "Exposed form" section in advanced.

3. There will be "Exposed form style" in Exposed form section.

4. Select "Better Exposed Filters" as exposed form style.

5. Add the checkboxes or radio buttons to the desired fields.

6. You are all done :).

What is contextual filter

Contextual filters work similarly to regular filters, but there is one important difference. Instead of setting a filter value manually, the value is fetched from variables sent programmatically to the view

The classic example of how contextual filter values are provided to views is by the view path. If a view has the path example.com/my-view, the URL example.com/my-view/story/22 will call the view along with two values for contextual filters (in this casestory and 22). But there are more ways of providing contextual filter values