Adding a pseudo field
Sometimes you need to display a field that have no input, like a static text or image on each node, or a Facebook like button, or whatever that does not have an input, but you still want to display it as a field, for that you have the hook hook_field_extra_fields.
To display a pseudo field you just need two functions (these functions I added to a custom module called wk_extra_fields)
<span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #FF8000">/**<br> * Implements hook_field_extra_fields().<br> */<br></span><span style="color: #007700">function </span><span style="color: #0000BB">wk_extra_fields_field_extra_fields</span><span style="color: #007700">() {<br> </span><span style="color: #0000BB">$extra </span><span style="color: #007700">= array();<br> </span><span style="color: #FF8000">// Just adding the psuedo field to the page node type.<br> </span><span style="color: #0000BB">$extra</span><span style="color: #007700">[</span><span style="color: #DD0000">'node'</span><span style="color: #007700">][</span><span style="color: #DD0000">'page'</span><span style="color: #007700">] = array(<br> </span><span style="color: #DD0000">'display' </span><span style="color: #007700">=> array(<br> </span><span style="color: #DD0000">'wk_foo' </span><span style="color: #007700">=> array(<br> </span><span style="color: #DD0000">'label' </span><span style="color: #007700">=> </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'WK: Foo'</span><span style="color: #007700">),<br> </span><span style="color: #DD0000">'description' </span><span style="color: #007700">=> </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'A foo field.'</span><span style="color: #007700">),<br> </span><span style="color: #DD0000">'weight' </span><span style="color: #007700">=> -</span><span style="color: #0000BB">5</span><span style="color: #007700">,<br> ),<br> ),<br> );<br> return </span><span style="color: #0000BB">$extra</span><span style="color: #007700">;<br>}<br><br></span><span style="color: #FF8000">/**<br> * Implements hook_node_view().<br> */<br></span><span style="color: #007700">function </span><span style="color: #0000BB">wk_extra_fields_node_view</span><span style="color: #007700">(</span><span style="color: #0000BB">$node</span><span style="color: #007700">, </span><span style="color: #0000BB">$view_mode</span><span style="color: #007700">, </span><span style="color: #0000BB">$langcode</span><span style="color: #007700">) {<br> </span><span style="color: #FF8000">// The field is showed in when using full view mode and on page node type.<br> </span><span style="color: #007700">if (</span><span style="color: #0000BB">$view_mode </span><span style="color: #007700">== </span><span style="color: #DD0000">'full' </span><span style="color: #007700">&& </span><span style="color: #0000BB">$node</span><span style="color: #007700">-></span><span style="color: #0000BB">type </span><span style="color: #007700">== </span><span style="color: #DD0000">'page'</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">$node</span><span style="color: #007700">-></span><span style="color: #0000BB">content</span><span style="color: #007700">[</span><span style="color: #DD0000">'wk_foo'</span><span style="color: #007700">] = array(<br> </span><span style="color: #DD0000">'#markup' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'<div class="wk-foo">Foo!</div>'</span><span style="color: #007700">,<br> );<br> }<br>}<br></span><span style="color: #0000BB">?></span></span>
So we use the function wk_extra_fields_field_extra_fields() to create the pseudo field for the page node type, after that we can activate it in the display tab, and choose where to show the field.
When we view the node, the function wk_extra_fields_node_view() is called and the content is displayed.
Image: "Air Guitar Championship" by AxsDeny. Attribution-NonCommercial-NoDerivs License