Drupal Quick Code
Action | Code |
---|---|
CREATE NEW NODE
(Basic) |
$node_title = 'Article One';
|
CREATE NEW NODE
(Associating references) |
$node_title = 'Article One';
|
NODE DELETE
(Returns NULL in both the case valid or invalid node ID) |
$nid = 4; // Say, node ID 4.
|
DEFINE CONSTANT |
const MY_CONSTANT_NAME = 'Drupal Drug';
Also, check Drupal Constants |
GET VARIABLE
(Returns variable value OR NULL in case not set) |
variable_get('my_module_my_variable');
|
SET VARIABLE
(Returns NULL) |
variable_set('my_module_my_variable', $value);
|
DELETE VARIABLE
(Returns NULL) |
variable_del('my_module_my_variable');
|
CREATE WEBFORM COMPONENTS Required Module: webform (Returns component ID on each iteration) |
$webform_node = node_load($webform_nid);
|
SUBMIT WEBFORM Required Module: webform (Returns submission ID) |
global $user;
|
/**
ReplyDelete* Unpublishes a node.
*/
function _node_unpublish($nid = NULL) {
// Check if NID an integer
$node = precheck_node($nid);
$msg = 'Node could not be unpublished.';
// Unpublish if node exists.
if ($node) {
$node->status = 0;
node_save($node);
$msg = 'Node unpublished successfully.';
}
drupal_set_message(t($msg));
}
/**
* Delete a node.
*/
function _node_delete($nid = NULL) {
// Check if NID an integer
$node = precheck_node($nid);
$msg = 'Node could not be deleted.';
// Unpublish if node exists.
if ($node) {
node_delete($nid);
$msg = 'Node deleted successfully.';
}
drupal_set_message(t($msg));
}
/*
* Returns node object for valid node id.
*/
function precheck_node ($nid) {
if (preg_match('/^[0-9]+$/', $nid)) {
return node_load($nid);
}
}
TIP: Find latest drupal version here 'https://www.drupal.org/download-latest/tar.gz'
ReplyDeleteglobal $base_url;
ReplyDelete$file = file_load($fid);
$file_uri = $file->uri;
$zipfile = substr(file_create_url($file_uri), strlen($base_url) + 1);
$extract_path = 'sites/default/files/template_html/html_' . $node->nid;
$zipfile = 'sites/all/file.zip';
$path = 'sites/all/extracted'
// Unzips a file.
function _unzip_html_files($zipfile, $path, $id = '') {
$zip = new ZipArchive;
$open = $zip->open($zipfile);
if ($open === TRUE) {
$extract = $zip->extractTo($path);
if ($extract === TRUE) {
$zip->close();
}
else{
watchdog('template-html', 'We cannot unzip the file:' . $id);
}
}
else{
watchdog('template-html', 'Could not open uploaded zip archive: ' . $id);
}
}
Drupal code search in all contributed modules.
ReplyDeleteExample search, node_insert
http://grep.xnddx.ru/