I am trying to print the node type in a tpl but not having much look the code I am using is: type != 'commissions'): ?>
Any ideas?
Cheers
There is a whole node object in the tpl file.Perhaps you can:
$node->type;
Try this
$node= node_load($nid); // $nid = your node ID
$type=$node->type;
then check ($type!='commissions')
Related
Hello!
I am trying to get the fields of views
There is a code:
$variables = module_invoke('views', 'block_view', 'news-block');
print render($variables['content']);
to display unit software.
Whether it is possible to obtain the field at such a conclusion. Rather get views across the fields from variable $variables
Advance all grateful
Sorry for my English.
You can use views_get_view_result function:
$view = views_get_view_result($name, $display_id); // returns an array conatining the result of the view
print_r($view);
If you prefer to get whole node objects, you can use node_load function with nids of each row:
$view = views_get_view_result($name, $display_id); // returns an array conatining the result of the view
$nodes = array(); // create an empty array to push each node objects
foreach ($view as $row) {
$node = node_load($row->nid); // get the node object by nid
$nodes[] = $node; // push node to $nodes array
}
print_r($nodes);
Also see: https://drupal.stackexchange.com/questions/32825/get-a-views-result-using-php-code-and-iterate-the-proper-way
I want to delete a property of a node, so I have written something like this -
var params={};
var propKey="somekey"+"#Delete";
params[propKey] = "some value";
params["_charset_"] ="utf-8";
$CQ.post("/path/to/my/node",params,null);
above code is not deleting from the node. Kindly advice!
some value should actually be null, otherwise the property has a value and won't be deleted. The type hint only works for the empty values, like null.
var params={};
params["somekey"+"#Delete"] = null;
params["_charset_"] ="utf-8";
$CQ.post("/path/to/my/node", params, null);
I am writing a Drupal custom module in which I create a node based on custom values. This is the code which creates node in proper manner.
global $user;
$node = new stdClass();
$node->type = 'my_node_type';
//$node->title = $nodeInfo->title;
node_object_prepare($node);
$node->language = LANGUAGE_NONE;
$node->uid = $user->uid;
$node->field_node_refrence_field['und'][0]['nid'] = $nid-of-reference-field;
$node = node_submit($node);
node_save($node);
I have the Node Autotitle module enabled for this content type. Due to that, the title is displayed as blank. I have checked the module, and I found that auto_nodetitle_set_title($node) sets the title. When I use this function in my code nothing happens.
Can anyone give me an idea on how to save the node with node_autotitle settings?
The code executed from auto_nodetile_set_title() is the following one. (The comments identifying parts of the code are mine.)
$types = node_type_get_types();
$pattern = variable_get('ant_pattern_' . $node->type, '');
// 1
if (trim($pattern)) {
$node->changed = REQUEST_TIME;
$node->title = _auto_nodetitle_patternprocessor($pattern, $node);
}
// 2
elseif ($node->nid) {
$node->title = t('#type #node-id', array('#type' => $types[$node->type]->name, '#node-id' => $node->nid));
}
// 3
else {
$node->title = t('#type', array('#type' => $types[$node->type]->name));
}
// Ensure the generated title isn't too long.
$node->title = substr($node->title, 0, 255);
// With that flag we ensure we don't apply the title two times to the same
// node. See auto_nodetitle_is_needed().
$node->auto_nodetitle_applied = TRUE;
The first control statement is executed if there is a settings for the title of that content type. If there isn't, and you are updating a module, then the second control statement is executed, otherwise it is executed the third one.
The title should never be empty, since the module always set it. The only time it could be empty is when Drupal doesn't have information about the content type used for the node; in that case $types[$node->type] would be NULL, but $types[$node->type]->name would raise the error "trying to access the property of something that is not an object."
I would use the following code, to save the node.
global $user;
$node = new stdClass();
$node->type = 'my_node_type';
node_object_prepare($node);
$node->uid = $user->uid;
$node->language = LANGUAGE_NONE;
$node->field_node_refrence_field[$node->language][0]['nid'] = $nid-of-reference-field;
$node = node_submit($node);
node_save($node);
auto_nodetitle_set_title($node);
node_save($node);
Since you are saving a new node, calling auto_nodetitle_set_title() before node_save() would not allow the function to execute the code marked with (2), and use the node ID for the title. Once auto_nodetitle_set_title() is called, you need to call node_save() to save the new title.
I'm trying to insert values for a node field from a custom module. The value will only be inserted from that module and then no operation for that node will be performed, so hook is not in my consideration. I've tried to directly insert values to the field_data_... and field_revision_... tables. but I've found that drupal also saves the values (serialized) in field_config and field_config_instance tables as blob. since I'm only inserting values in two tables drupal is not reading values I've inserted for nodes. I couldn't understand the serialization stored in DB. so I'm searching for a method that will help me to insert values through API or any other neat way.
any help on the thing I'm trying to accomplish would be great. Thank you
EDIT:
after taking look at serialized content of field_config table I understood that the serialized data is just field configuration and get inserted into the table on first save. My problem solved by saving the first value through admin/content and now my direct DB inserted data is available to the node. this is the serialized data I've got:
a:7:
{s:12:"translatable";
s:1:"0";
s:12:"entity_types";
a:0:{}
s:8:"settings";
a:3:
{s:9:"precision";
s:2:"10";s:5:"scale";
s:1:"2";
s:17:"decimal_separator";
s:1:",";
}
s:7:"storage";
a:5:
{s:4:"type";
s:17:"field_sql_storage";
s:8:"settings";
a:0:{}
s:6:"module";
s:17:"field_sql_storage";
s:6:"active";
s:1:"1";
s:7:"details";
a:1:
{s:3:"sql";
a:2:
{s:18:"FIELD_LOAD_CURRENT";
a:1:
{s:22:"field_data_field_total";
a:1:
{s:5:"value";
s:17:"field_total_value";
}
}
s:19:"FIELD_LOAD_REVISION";
a:1:
{s:26:"field_revision_field_total";
a:1:
{s:5:"value";
s:17:"field_total_value";
}
}
}
}
}
s:12:"foreign keys";
a:0:{}
s:7:"indexes";
a:0:{}
s:2:"id";
s:2:"34";
}
Hope this will help you.
$node = new stdClass();
$node->uid = 1;
$node->name = 'admin';
$node->type = 'page';
$node->language = 'und';
$node->title = 'Your title';
$node->status = 1;
$node->promote = 0;
$node->sticky = 0;
$node->created = timestamp;
$node->field_description = array(
'und' => array(
array(
'value' => 'asdasd'
)
)
);
$node->nid = 1; // define nid if you wish to update existing node
// if you wouldn't define $node->nid then new node would be created,
// otherwise node would be updated with you data provided for all
// fields which you'll list here.
...
// other node's fields
node_save_action($node);
I keep getting a NullReferenceException at this line UserRoot.Element("User_ID").Value = User.User_ID.ToString();
What exactly am I doing wrong?
Here's the majority of the code for that method
if (File.Exists(Path2UserDB + User.User_ID.ToString() + ".db") == false)
{
File.Create(Path2UserDB + User.User_ID.ToString() + ".db");
}
XElement UserRoot = new XElement("User");
UserRoot.Element("User_ID").Value = User.User_ID.ToString();
UserRoot.Element("Full_Name").Value = User.Full_Name;
UserRoot.Element("Gender").Value = User.Gender;
UserRoot.Element("BirthDate").Value = User.BirthDate.ToString();
UserRoot.Element("PersonType").Value = User.PersonType.ToString();
UserRoot.Element("Username").Value = User.Username;
UserRoot.Element("Password").Value = User.Password;
UserRoot.Element("Email_adddress").Value = User.Email_Address;
XDocument UserDoc = new XDocument();
UserDoc.Save(Path2UserDB + User.User_ID.ToString() + ".db");
Thanks
I know that saving Usernames and Passwords in plain text is incredibly unsafe, but this is only going to be accessed by one process that I will eventually implement strong security
The Element("User_ID") method returns an existing element named <User_ID>, if any.
Since your XML element is empty, it returns null.
You should create your XML like this:
var userDoc = new XDocument(
new XElement("User",
new XElement("User_ID", User.User_ID),
new XElement("Full_Name", User.Full_Name),
new XElement("Gender", User.Gender),
...
)
);
Alternatively, you can call the Add method to add a node to an existing element.
You are getting this error, because there is no XML element called User_ID under UserRoot to set its value. If you comment it out, you will get the same error on the next line and so on for every other Element, since you haven't added Elements with thos names. To create the tree that you want, try this:
XElement UserRoot =
new XElement("User",
new XElement("User_ID", User.User_ID.ToString()),
new XElement("Full_Name", User.Full_Name),
new XElement("Gender", User.Gender),
new XElement("BirthDate", User.BirthDate.ToString()),
new XElement("PersonType", User.PersonType.ToString()),
new XElement("Username", User.Username),
new XElement("Password", User.Password),
new XElement("Email_adddress", User.Email_Address)
);
The following MSDN link on XML Tree Creation with XElement will be of help.
You want to check if the value is null or empty before running methods on it.
if(!String.IsnullorEmpty(User.User_ID))
UserRoot.Element("User_ID").Value = User.User_ID.ToString();