snipt

Ctrl+h for KB shortcuts
Sign up for : store and share your own code snippets.
#47975

PHP

Get values for a given field

<?php $items = field_get_items('node', $node, 'field_myfield'); ?>
https://snipt.net/embed/09efaf02803e1fb57731f5f45a66da9b/
https://snipt.net/raw/09efaf02803e1fb57731f5f45a66da9b/
09efaf02803e1fb57731f5f45a66da9b
php
PHP
1
2015-07-04T15:13:50
True
False
/api/public/snipt/47975/
get-values-for-a-given-field
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> <span class="nv">$items</span> <span class="o">=</span> <span class="nx">field_get_items</span><span class="p">(</span><span class="s1">&#39;node&#39;</span><span class="p">,</span> <span class="nv">$node</span><span class="p">,</span> <span class="s1">&#39;field_myfield&#39;</span><span class="p">);</span> <span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
"drupal 7", field, node
#47974

PHP

Get the current node

<?php $node =& menu_get_object(); ?>
https://snipt.net/embed/a146b9faccc8b5cb4ab57be3b6881faf/
https://snipt.net/raw/a146b9faccc8b5cb4ab57be3b6881faf/
a146b9faccc8b5cb4ab57be3b6881faf
php
PHP
1
2015-07-05T18:28:07
True
False
/api/public/snipt/47974/
get-the-current-node
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> <span class="nv">$node</span> <span class="o">=&amp;</span> <span class="nx">menu_get_object</span><span class="p">();</span> <span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
"drupal 7", node
#35625

PHP

Load author of node and render custom fields from user profile in D7

1
2
3
4
5
6
<?php
// load author
// user this inside of custom node template, e.g. node--blog.tpl.php
$author = user_load($node->uid);
?>
<div class="blog-author-name"><h3>Posted by <?php print $author->field_user_first_name['und'][0]['value'].' '.$author->field_user_last_name['und'][0]['value']; ?></h3></div>
https://snipt.net/embed/39847a535de37041f0e17950e8e00d8e/
https://snipt.net/raw/39847a535de37041f0e17950e8e00d8e/
39847a535de37041f0e17950e8e00d8e
php
PHP
6
2015-07-06T08:02:59
True
False
/api/public/snipt/35625/
load-author-of-node-and-render-custom-fields-from-user-profile-in-d7
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a><span class="c1">// load author</span> </span><span id="L-3"><a name="L-3"></a><span class="c1">// user this inside of custom node template, e.g. node--blog.tpl.php</span> </span><span id="L-4"><a name="L-4"></a><span class="nv">$author</span> <span class="o">=</span> <span class="nx">user_load</span><span class="p">(</span><span class="nv">$node</span><span class="o">-&gt;</span><span class="na">uid</span><span class="p">);</span> </span><span id="L-5"><a name="L-5"></a><span class="cp">?&gt;</span><span class="x"></span> </span><span id="L-6"><a name="L-6"></a><span class="x">&lt;div class=&quot;blog-author-name&quot;&gt;&lt;h3&gt;Posted by </span><span class="cp">&lt;?php</span> <span class="k">print</span> <span class="nv">$author</span><span class="o">-&gt;</span><span class="na">field_user_first_name</span><span class="p">[</span><span class="s1">&#39;und&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;value&#39;</span><span class="p">]</span><span class="o">.</span><span class="s1">&#39; &#39;</span><span class="o">.</span><span class="nv">$author</span><span class="o">-&gt;</span><span class="na">field_user_last_name</span><span class="p">[</span><span class="s1">&#39;und&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;value&#39;</span><span class="p">];</span> <span class="cp">?&gt;</span><span class="x">&lt;/h3&gt;&lt;/div&gt;</span> </span></pre></div> </td></tr></table>
"drupal 7", "user profile", author, node
#35183

PHP

Devel output

<?php 
  dpm($node)
https://snipt.net/embed/67974c623097053ec70c4c0d61dcc5ce/
https://snipt.net/raw/67974c623097053ec70c4c0d61dcc5ce/
67974c623097053ec70c4c0d61dcc5ce
php
PHP
2
2015-06-23T19:14:33
True
False
/api/public/snipt/35183/
devel-output
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a> <span class="nx">dpm</span><span class="p">(</span><span class="nv">$node</span><span class="p">)</span> </span></pre></div> </td></tr></table>
drupal, node
Copyrighted, illegal, or inappropriate content? Email nick@snipt.net.