By adam640kb on
Is it possible to use node_load() to check for a value in a cck field?
Something like:
$node_query['type'] = 'myccktype';
$node_query['field_mycckfield'] = array ( 0 => array ('value' => $mycckvaluetocheck ));
$node = node_load($node_query);
if (!$node)
{
// Node doesn't exist..
}
The above doesn't work for me, so essentially if it's possible to query CCK fields using node_load() what is the proper format for my $node_query above?
Comments
node_load() only directly works with core tables
node_load() only directly works with core tables so what you wish to do will not work using node_load.
Oh?
Oh?
produces output which includes:
["field_myfield"]=> array(1) { [0]=> array(1) { ["value"]=> string(1) "X" }In conclusion, it's in there somewhere, but it looks like cck fields are abstracted into arrays that yet perplex me. But they're in there.
Roy Steves
Yes, but...
Yes, but if you look in the documentation (api.drupal.org) and read the node_load function, you'll see it only looks in the core tables (node and node_revisions), not in the tables created by CCK. Sad but true.
Here's what I do in a module that needs to load a CCK node (I think I adapted most of this from the Taxonomy Import module):
Someday, I'll make it more generic so I don't keep writing a new one for each odd content type I need to search (requires dynamically naming the static $cache variable, among other things).
Then, your code could just do ...
Place the function in a module and activate it, and you'd be able to use it in a page or view or block's php code block too, I presume. Haven't tried it yet.
What I usually use it for is during custom node imports from flat files and other odd data we need. If the node exists, I pass the nid in the node array to node_save and it decides to do an update; if not, it's empty and node_save does an insert. Remember that if you re-use a CCK field in another content type, that field will be moved to another table, where the multiple content types share it. There's probably some cool way to deal with that problem, but I haven't bothered yet. We have a policy of NOT re-using CCK fields for other content types.
If you're not doing repeated hits on this function (as I do), the caching is probably unnecessary and could be deleted.
Hope that helps someone
--eon--
This can work and is useful
when you customize your node.tpl.php.
Since this is an old post you may have already thought of this long ago, but just in case...
I'm using it for a nodereference field that I do not want to link to its node -- instead I want it to display an image and a hyperlink to an external url.
So in node.tpl.php I capture the nodereference field, get the node id it references, load that node, then output the value of a CCK field "field_myfield" (which contains my hyperlink and image) like this:
and voila, it prints the contents of my custom field.
Hope this is helpful.
Hello, It looks like exactly
Hello,
It looks like exactly what I'm looking for:
I'm using Content template module and I'm looking for to use a node reference:
node-type A: field_title_france
node-type B: node reference A, the node referrence cck fielk name is: field_lien_fiche
Content template node-type B:
but how to use the node reference in it?? (field_title_france is a cck from node A)
I try:
Any idea?
Thanks :)
Try this...
Put this in the template for node-type B:
Thanks a lot!! It works! :)
Thanks a lot!! It works! :)
You're welcome.
Basically, your original content type B code was just looking at the node ID of itself, which is of no interest when you want information about the type A node being referenced. What my code did was look at the node ID of the type A node that was being referenced, and then extract information from there.
Thanks for the explanation!
Thanks for the explanation! This code is perfect I think :)
Strange, I'm using Roles with
Strange, I'm using Roles with these fields and all of them appear
Users have to choose one role, and will only see the title in their role.
Any idea why roles don't have effect here?
How about...
I guess the roles rules aren't being applied because you're only extracting the value of a field, and not all of the extra little fields that are normally rendered along with it.
You could try a code like this...
I know it's not the optimal solution to hand-code each role into your code, but it's all I can think of. There's probably an easier and less-manual way, though.
I try this but doesn't
I try this but doesn't work.
I try change something in this code, and it was a big mystake.
I can't go back to the contemplate edit page... I can't go to the node page. I've got:
Fatal error: Call to undefined function array_value() in /home/pariscin/www/sites/all/modules/contemplate/contemplate.module(834) : eval()'d code on line 22
How to rechange the code? lol
Without deleting the template?
So you typed that code into
So you typed that code into contemplate and now you can't get back to the contemplate edit page for that specific template, or you can't get back to the contemplate page that lists all of the contemplate templates? (In other words, is your contemplate installation completely broken now, or does it just have to do with this one content type?)
can't get back to the
can't get back to the contemplate edit page for that specific template,
but don't worry, I deleted it and rebuilt it in 5 min. :)
I tried this code too:
The php code shouldn't be good.
Maybe something like:
if $user->roles[5] print...
I'm not an expert in php, I began a few month ago with php :)
Try this code instead
Try this code instead:
nothing appears at all
nothing appears at all
<?php global $user;
This works, but all roles appear.
in_array to array
Using (array('espagne',
Using (array('espagne', array_values($user->roles))) or (array('espagne', $user->roles)) is the same.
Ok, how about this...
nothing appears :)
nothing appears :)
but why if($role == "france")
but why if($role == "france") ?
It shouldn't be if($user->roles[5]) ?
Funny, I try this: <?php
Funny, I try this:
I've got: field_title_france[0]['value'] 3 times
the reason...
I was just assuming that one of the role names was "france". Let's assume you have a role named "france" with a role id of 8. Either of these 2 methods should work I believe:
<?php global $user;
works great!!
I had really appreciate your help, thanks a lot :)
Note there is no need for the
Note there is no need for the loop, you could just write
oh ok! Thanks :)
oh ok! Thanks :)
hum Nothing appears
The full snippet
Don't forget the line that reads $noderefnode = node_load($field_lien_fiche[0][nid]);
So, the code would be:
Oh sure forgot it! Thanks.
Oh sure forgot it! Thanks.
Just a last question as you
Just a last question as you look like to know very well nodereferrence stuff :)
Is it possible to use a nodereference with C from A?
A contains field_title_france
B reference to A, display field_title_france
C reference to B, display field_title_france possible??
Without using a new nodereferrence field with C and A.
Re: Just a last question as you
This is in response to your "Just a last question as you", but I'm bringing it over to the left so there's more room...
Yes, that's possible. The code would look like this:
Notes:
-field_noderef_inc_tob is the node reference field in content type C that references a type B node
-field_noderef_inb_toa is the node reference field in content type B that references a type A node
-field_a_field is the field in content type A that you want to display in content type C
-I created the variable $nodebdata to temporarily store the data about the referenced type B node
-I created the variable $nodeadata to temporarily store the data about the referenced type A node
-You'll also have to add in the permissions code like we discussed above. Same method as above. (Not including it here so as not to confuse people reading this in the future).
Hello,It doesn't work, crash
Hello,
It doesn't work, crash the template page again (can't get back to the contemplate edit page for that specific template).
Fatal error: Cannot use object of type stdClass as array in /home/pariscin/www/sites/all/modules/contemplate/contemplate.module(834) : eval()'d code on line 32
My node referrence cck field is "field_lien_fiche".
I'm using the same node referrence field in node type B and node type C. Could be a problem with this code? (I don't think so, but i'm not an expert.)
Try this...
I think I accidentally thought node_load would load that data as an array. Kind of stupid of me to think, seeing as how with my original node_load I treated the loaded data as an object and not as an array. This code should work I think...
Damm, you're good!!
Damm, you're good!! lol
It works perfectly!
Thanks! :)
No problemo
:)