Posts

Showing posts from August, 2017

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!