Blog Tags
Blog Archiv
- April, 2010 (1)
- Dezember, 2009 (2)
- November, 2009 (4)
CCK Feld sprich mit mir
Drupal 5.x, 6.x
Um CCK Felder in Templates, in Themefunktionen oder anderweitig ansprechen zu können ist nur wenig Code notwendig.
CCK Feld ausgeben
Die Syntax ist Folgende. Beispiel mit einem CCK Feld foobar:
<?php
$node->field_foobar[0]['value'];
?>
Handelt es sich um ein Feld das in einem Node Profile Inhaltstyp existiert muss vorher das Nodeobjekt geladen werden.
"Node Profile" Nodeobject laden
Nodeobjekt eines Node Profile Inhaltstyp laden. Der Inhaltstyp ist "address":
<?php
global $user;
$nodeprofile = nodeprofile_load('address', $user->uid);
?>
Dann das Feld ansprechen:
<?php
$nodeprofile->field_foobar[0]['value'];
?>
Die Object-Variable $node, das Nodeobject
Um CCK generierte Felder in Nodetypes ansprechen zu können muss das Nodeobject, die Object-Variable $node, auch verfügbar sein. Dies ist nicht in allen Situationen der Fall. Ein einfaches
<?php
print $node
?>
erzeugt eine Ausgabe mit der man erkennen kann ob das Nodeobject verfügbar ist oder nicht.
Wird mit print $node eine Ausgabe erzeugt, also das Nodeobject ist verfügbar, kann man sich den Inhalt des Nodeobjectes anzeigen lassen um zu sehen wie man die einzelnen Werte des Nodeobjectes ansprechen kann.
Um die Anzeige des Nodeobjectes zu realisieren wird die PHP Funktion print_r() verwendet. Um bei der Verwendung eine lesbare Anzeige zu erhalten wird die Anzeige der PHP Funktion etwas gepimpt.
<?php
print '<pre>';
print_r($node);
print '<pre>';
?>
zeigt ob die Verfügbarkeit gegeben ist. Erfolgt dabei keine Ausgabe ist das Nodeobject nicht verfügbar und es erst muss geladen werden um es verwenden zu können.
Nodeobject laden
Das Laden des Nodeobjectes kann/muss in Zusammenhang mit der Kenntnis einer Node ID erfolgen. Verwendet wird dazu die Drupal Funktion node_load(). In der Regel ist eine Node ID verfügbar wenn eine Node angezeigt wird. In dieser Situation kann man die Node ID aus der URL mittels der Drupal Funktion arg() sozusagen aufgreifen.
Als Beispiel ein Block-Template oder der PHP Codebereich in einem Block. Die Beispiel URL lautet: example.com/node/14
Um das Nodeobject zu laden wird folgenderPHP Code verwendet.
<?php
if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
$node = node_load(arg(1));
}
?>
Dem URLpart node entspricht arg(0) und dem URL Part 14 entspricht arg(1). Auf die Prüfung mit if() sollte man nicht verzichten wenn die Situation eintreten kann das bei Verwendung des Codes eine URL zur Anzeige kommt die nicht dem Schema example.com/node/14 folgt.
Nicht immer Nodeobject laden
In den Fällen in denen eine Node ID bekannt ist, beispielsweise in einem View mit einem Argument als Node ID, muss das Nodeobject logischerweise nicht erst geladen werden um es ansprechen zu können.
Testfrage
Befindet man sich in einem Nodetype Template kann man direkt arg(1) verwenden - auch ohne zu prüfen. Denn wenn eine Node angezeigt entspricht die URL immer dem Schema example.com/node/14. Richtig?
Und was ist bei der Anzeige von Nodes in Teaserlisten wenn die URL beispielsweise dem Schema example.com/taxomomy/term/32 entspricht?
CCK Felder unterschiedlich ansprechen
Entweder mit
<?php
$node->field_foobar[0]['value'];
?>
oder mit
<?php
$node->field_foobar[0]['view'];
?>
Ob man die erste oder zweite Variante verwendet oder verwenden muss hängt davon ab ob es sich bei dem anzusprechenden Feld um ein Feld handelt das nur einen einzelnen Wert enthält wie beispielseise ein einzeiliges Textfeld oder ob es sich um ein Feld handelt das mehrere Werte enthält wie beispielsweies ein Bild- oder Linkfeld.
Links
Drupal API: node_load()
Drupal API: arg()
PHP Funktion: print_r()
Kommentare
I cannot tell you official
I cannot tell you official ugg boots how strong my love for you was back then, abercrombie london
abercrombie
abercrombie and fitch
abercrombie uk
???? ??
tiffany uk
abercrombie
Abercrombie outlet
Abercrombie clothing
ed hardy uk
love kills slowly
abercrombie outlet
abercrombie clothing
abercrombie and fitch uk
abercrombie fitch uk
Abercrombie & fitch uk
???? ??
???? ??
abercrombie uk
abercrombie and fitch uk
Abercrombie & Fitch UK
abercrombie fitch uk
when I returned to you on leave in the September, feeling Australia ugg boots battered, bruised and fragile.
Natural slimming center
Natural slimming center offer 100% original slimming pills.slimming coffee,slimming tea,Such asgreen coffee 800.
slimming cream,slimming underwear,,
slimming diet,slim tea,herbal slimming tea,
sex underwear, to world wide with very competitive price and best quatity.Such as
lishou slimming, wu long slimming tea,lida daidaihua,super slim pomegranate weight loss,superslim.
cheap ed hardy
cheap ed hardy clothing
cheap ed hardy shoes
ed hardy shoes
cheap ed hardy bags
ed hardy handbags
ed hardy sunglasses
ed hardy caps
ed hardy belts
ed hardy t-shirts
nfl football
nfl football jerseys,
authentic nfl jerseys,
nfl replica jerseys,
nfl throwback jerseys,
custom nfl jerseys,
reebok nfl jerseys,
hockey jerseys,
team hockey jerseys,
baseball jerseys,
basketball jerseys,
will nich funktionieren -.-
hi!
vielen dank für diesen artikel, der hat mir sehr weiter geholfen, nur leider geben die feld variablen bei mir einfach nichts aus. habe das hier http://www.drupalcenter.de/node/23562#comm... schon mal beschrieben. vielleicht kannst du mir ja helfen, wäre seeehr nett, bin langsam am verzweifeln :(
liebe grüße,
thomas
akljewajfwioefu
hollister uk
hollister
hollister clothing
hollister clothes
hollister london
hollister sale
hollister outlet
hollister online
billig kamagra
billig kamagra
Hi admin, greetings from
Hi admin, greetings from Munich, Germany! I cannot agree more. We are faced with about the same problem really and baby phat shoes what's more for us: since we are not U.S. based, most of the Drupal developers out there who are in the U.S. (and these seem to be the majority) seem to not trust at all a guess shoes contractor from outside the U.S. - One particular high profile Drupal dev guy you hear about constantly on Drupal.org and JUST the guy for a particular development task said: hey, you are not in the U.S., who tells me if I get my money. Well. The other one was plain arrogant, both in his mails and in his price ideas for the adaption of the most simple stuff. Both - and this actually alienated me the most about them - shared an attitude as if I had made some rather indecent proposals to them...
Well, I'd be very happy if you could contact me directly by email, since we might be able to help each other in what anne klein shoes we are doing a lot. You got my address from my profile I'd be happy if you could drop me a short line!
BTW: you might want to check your access settings for this site: it appears I have access to editing YOUR blog post and access to the outliner as well...
Hope to talk to you soon, Steve.
Security Problem
könntest du dazuschreiben dass es besser ist [safe] anstatt [value] zu benutzen?
Der Sinn ist ja klar :)
Definition "ansprechen
Könntest Du den Begriff "ansprechen" näher definieren?
"ansprechen" heißt nicht "anzeigen" oder sehe ich das falsch?
wie muß der code lauten, um ein auszugeben (sprich im browser anzeigen zu lassen)?
Korrekt, ansprechen bedeutet
Korrekt, ansprechen bedeutet nicht anzeigen.
Im Browser wird aus PHP Code heraus mit print angezeigt.
<?php
print $node->field_foobar[0]['safe'];
?>