Posts

Showing posts from 2017

Linux - search all extension available in a folder(recursively)

To search all extension available in a folder(recursively) Navigate to the target directory cd target/directory find $directory -type f -name "*.docx" Note: find command works recursively by default

Linux - How to delete all files recursively for sub-folders with out deleting the folder/sub-folder

To List all the files recursively form the patent folder and sub-folder, cd /target/directory find . -type f To Delete all the files recursively form the patent folder and sub-folder, cd /target/directory find . -type f -delete To Delete all the sub-folders & files recursively form the patent folder, cd /target/directory find . -type rf -delete

D8 - Derive the route_name for the views created page

You can derive the views route_name from the below pattern view.{view_id}.{display_id} {view_id} - Basically the machine name appears in the URL when you edit the view. {display_id} - Hope the tab of every display to find the display ID(eg., page_1, page_2 etc.,)

D8 - Programmatically Redirect in Custom module

Use the below code use Symfony\Component\HttpFoundation\RedirectResponse; use Drupal\Core\Url; return new RedirectResponse(\Drupal::url('{route_name}')); {route_name} - Name given while defining the path in .routing file. To derive the route_name of a views created page refer the below link, http://romelchrist.blogspot.com/2017/09/d8-derive-routename-for-views-created.html

D8 - How to Get Current node object in content view/edit page

$current_node = \Drupal::routeMatch()->getParameter('node');

D8 - Attach file programmatically to a node

Scenario 1: If the file resides on the other server/website. Try the below code, use \Drupal\node\Entity\Node; /** Create file object from remote server/website URL. */ $data = file_get_contents('https://www.example.com/sites/default/files/logo.png'); $file = file_save_data($data, 'public://logo.png', FILE_EXISTS_REPLACE); // Create node object with attached file. $node = Node::create([   'type'        => 'article',   'title'       => 'Sample test',   'field_image' => [     'target_id' => $file->id(),   ], ]); $node->save(); Refer " file_save_data " API for more options/overide functions Scenario 2:  If the file resides on the same server/website. Try the below code, use \Drupal\node\Entity\Node; use \Drupal\file\Entity\File; // Create file object from a locally copied file. $uri  = file_unmanaged_copy('public://source.jpg', 'public://destination.jpg', ...

D8 - Programmatically Delete configuration settings

Execute the below code in hook_preprocess_page or hook_preprocess_html Drupal::configFactory()->getEditable('simplesamlphp_auth.settings')->delete();

D8 - Get Public and private file path in custom module/code

1) To get the Public directory path in custom module/code $Public_base_path = \Drupal\Core\StreamWrapper\PublicStream::basePath(); 2) To get the Private directory path in custom module/code $Private_base_path = \Drupal\Core\StreamWrapper\PrivateStream::basePath();

GIT - Filename too long error while pulling on git for windows

Execute the below command: git config --system core.longpaths true or git config core.longpaths true

GIT - Remove all uncommitted changes

This will unstage all files you might have staged with git add:   git reset   This will revert all local uncommitted changes (should be executed in repo root):   git checkout .   You can also revert uncommitted changes only to particular file or directory:   git checkout [some_dir|file.txt]   Yet another way to revert all uncommitted changes (longer to type, but works from any subdirectory):   git reset --hard HEAD   This will remove all local untracked files, so only git tracked files remain:   git clean - fdx   WARNING: -x will also remove all ignored files!

Programmatically disable a module - Drupal 8

You can run the following code using  drush eval  or may be using the Devel module's provision to  Execute PHP Code . $module_data = \Drupal::config('core.extension')->get('module'); // Unset the modules you do not need.  unset($module_data['dblog']);  // Write the configuration.  \Drupal::configFactory()->getEditable('core.extension')->set('module', $module_data)->save();

D8 - Programmatically hack(login) into Drupal 8 - Custom Login

Here you go - For who ever(developer) think to force login as administrator. $user = \Drupal\user\Entity\User::load(1); user_login_finalize($user); Add the above code in *.theme file in current/default site theme.

How to save value for a field to node or user - Drupal 7 :: entity module

Introduction The contributed  Entity API  module provides wrapper classes that make dealing with the values of an entity's properties and fields easier. If you need to save a text or numeric value into a field you must pass the value as below, <?php $node = node_load($nid);//Node ID $node_wrapper = entity_metadata_wrapper('node', $node); $var = $node_wrapper->field_age->value() + 1; $node_wrapper->field_age->set($var); $node_wrapper->save(); ?> Note: to update the user value you can use user_load($uid); If you need to save a file into a field you must pass the file object or an array with a ``fid`` key: <?php   $node = node_load($nid); //Node ID   $containing_node = entity_metadata_wrapper('node', $node);   // Load the file object in any way   $file_obj = file_load($fid);   $containing_node->field_attachment_content->file->set( $file_obj );   // ..or pass an array with the fid   $co...