Adding same name restriction to Jackrabbit nodes - jackrabbit

I want to create a node that has a restriction about same name siblings. I have used a CND file for that.
[cog:example]
- * (undefined) multiple
- * (undefined)
- cog:name (string)
= 'Example Node Name'
mandatory
- cog:description (string)
= 'Description Goes Here'
autocreated
Then when I tests adding of the nodes:
Node root = session.getRootNode();
Node projects = root.addNode("projects");
Node exampleNode = projects.addNode("example 1","cog:example");
exampleNode.setProperty("cog:name", "example name");
session.save();
Node exampleNode2 = projects.addNode("example 1","cog:example");
exampleNode2.setProperty("cog:name", "example name");
session.save();
dumpToConsole(projects);
Adding the second node did not throw ItemExistsException exception, and dumping the node shows the second node (example 1[2]). Can you please tell me how to make a restriction so that same name nodes are not allowed?

You set same name sibling restrictions via the parent. So you can add eg.
[nt:parent]
+ * (cog:example)
then set the type of projects
Node projects = root.addNode("projects", "nt:parent");
Works for me.

Related

Drupal 7: Get all content items attached to all terms in a vocabulary sorted by date

Just like the title says. I have a vocabulary with several terms. I'd like to be able to get all items tagged by each of the terms in the vocabulary sorted by date created in one giant list. Is this possible with the built-in taxonomy API or am I going to have to build something custom?
As far as I know, there is no function/method in Drupal API will get that for you. I believe you will have to make your own.
When you say 'items' if you mean nodes then the function you are looking for is taxonomy_select_nodes().
https://api.drupal.org/api/drupal/modules%21taxonomy%21taxonomy.module/function/taxonomy_select_nodes/7
This function returns an array of node ids that match have the term selected.
/**
* Get an array of node id's that match terms in a given vocab.
*
* #param string $vocab_machine_name
* The vocabulary machine name.
*
* #return array
* An array of node ids.
*/
function _example_get_vocab_nids($vocab_machine_name) {
$vocabulary = taxonomy_vocabulary_machine_name_load($vocab_machine_name);
if (!$vocabulary) {
return array();
}
$terms = taxonomy_get_tree($vocabulary->vid);
if (!$terms) {
return array();
}
$nids = array();
foreach ($terms as $term) {
$term_nids = taxonomy_select_nodes($term->tid, FALSE);
$nids = array_merge($nids, $term_nids);
}
return $nids;
}

extjs 4 tree select a specific node by two internal id

We can search a record in ExtJs 4 TreeStore by internal id using following code
var record = tree.getRootNode().findChild('id_name','XYZ',true);
Lets say I want to search a record where id_name should be XYZ and age 10 10.
Do we have a method which allows user to search on more than one internal Id's?
Well, the id, whether it is called "id" or "id_name", must be unique for each node in the tree so there is always only one node returned by findChild and no other search condition is needed.
Use findChildBy here:
var record = tree.getRootNode().findChildBy(
function(node) {
return node.get('id_name') == this.requiredIdName && node.get('age') == this.requiredAge;
},
{
// Putting the search criteria stuff in the context used for the function
requiredAge: 10,
requiredIdName: 'abc'
},
// true to do a deep search, false to search only on first level
true
);

Lua - How to check if list contains element

I need some help with my lua script for a game. I need to check if my inventory in the game contains any id from a list.
Here's a piece of my list:
local Game_Items = {
{id = 7436, name = "angelic axe", value = 5000},
{id = 3567, name = "blue robe", value = 10000},
{id = 3418, name = "bonelord shield", value = 1200},
{id = 3079, name = "boots of haste", value = 30000},
{id = 7412, name = "butcher's axe", value = 18000},
{id = 3381, name = "crown armor", value = 12000}
}
The following code might look a bit weird since you don't know what it's for, but it's basically this: the list above is a list of items in my game, and inside the game theres an inventory where you can keep items and stuff. Now I want to check if my inventory contains any of those IDs.
I tried adding 2 of the id's manually and it worked, but my list of items contains over 500 items in total and I don't want to write them all out. Is there a way to put the whole list and check if it's in there somehow?
if not table.contains({ 3035, 3043, Game_Items[id] }, tempItemCounter.id) then
This is what I tried so far. Those two first id's work 3035 and 3043, then I tried all my whole list and only check the Ids. but I dont know how to do that. That code does not work. Could anyone just help me include the whole list of id's in the table.contains ?
Basically wanna include my whole list in that line, without typing out all IDs manually.
Shouldn't Game_Items[id] work? Doesn't that mean all the "id" inside "Game_Items"?
Thanks!
No it doesn't mean that. If foo is a table, then foo[id] looks for a field in foo that is called whatever id refers to, such as a string (so if id is 1 you will get foo[1], if id is "bar" you will get foo.bar, etc).
You can't do it in one line, but you can create a function that will allow you to write your if condition. I'm not sure what tempItemCounter is but assuming that your inventory is a map of keys to entries of the form
inventory = {
[1234] = {....},
[1235] = {....},
...
}
where each integer key is unique, and assuming you want true only if all items are in inventory, then you could do this:
function isAllInInventory(items, inventory)
for i,item in ipairs(items) do
if inventory[item.id] == nil
return false
end
end
return true
end
if isAllInInventory(Game_Items, inventory) then
...
end

How to save node with node_save() and node_autotitle module in custom module?

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.

Inserting node field value programmatically in drupal 7

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);

Resources