drupal 7 field collection, how to access elements from template? - drupal

I have a view block that takes pulls a field collection. The field collection can contain unlimited number of items, where each item has two fields (url and logo).
When I kpr(get_defined_vars) within block--views--my-view-block-block.tpl.php I get the following :
as you can see $content is just a string, so I cannot access the field collection items. I can access and template each field within field collection just fine, but what I need to do is theme each Element that contains a link and logo. Does anyone have any ideas how do accomplish this?

Just try to use below portion of the content variable.
<?php print render($content['field_video_file_col']); ?>
Instead of "field_video_file_col" , replace the field name with your link and logo and use
render function.

ended up creating a view tpl and using

Related

Alter field text of the view in Drupal View

I have a view for taxonomy term. And show the list of nodes. I need to alter field text (link element). I try to use views_pre_render hook. in it i do something like this:
foreach ($view->result as $key => &$result);
$result->field_field_show_buy_tickets[0] (it s my field) and in this array I have ['raw'], ['rendered'] and I need to change link title.
It's not very clear from your question what you're trying to achieve.
Assuming you want to change the link text in a view, you could go to the field configuration/settings in your view and try using rewrite options and replacement patterns
Alternatively you could look at views-view-fields.tpl.php and/or template_preprocess_views_view_fields.
Also, check out this discussion

Drupal 7: Hiding labels with empty fields when viewing a node

How do I hide labels that have empty fields when viewing the actual node of a certain content type?
I'd really appreciate anyone's help, thanks for your time.
Another way you could achieve this is by using a custom template file that would apply to all nodes of that content type.
Make sure that node.tpl.php exists in your sites/all/themes/[mytheme] directory first. This template must exist before other custom templates can be called.
Make a copy of your node.tpl.php and name it node--[contenttype].tpl.php (without the brackets).
If you have the Devel module enabled, you can throw a dpm($content); into the file to find out the name of the field you are trying to hide. Or you could look at the content type itself.
Once you have the name of the field, you can now insert this code before the print render($content); statement:
if (empty($content['my_field'])) {
unset($content['my_field']);
}
Clear the cache, and your field will only appear if there is a value stored.
By default, the labels of empty fields are hidden, maybe there's still a 'non breaking space' or some other leftover in the field?
You have to check the difference between an existing node where the problem occurs and a new node where you don't touch the particular field.
Set unwanted labels display hidden in nl/admin/structure/types/manage/selected_content_type/display
I would like to correct first answer. In node.tpl.php we should check #markup instead of field array:
if (empty($content['field_vac_req'][0]['#markup'])) {
unset($content['field_vac_req']);
}
instead of
if (empty($content['my_field'])) ...
If the content type has a lot of fields looping this worked for me:
foreach($content AS $key => $values) {
if (!empty($content[$key][0]['#markup'])) {
print render($content[$key]);
}
}

Drupal 6: print all body field content to node template file

I tried to display body content with:
<?php print $node->content['body']['#value']; ?>
However, it doesn't display all body content, it just display first paragraph of body content, sometimes 2 paragraph if it is short :/
I need to print all body. how can I do that?
Thanks a lot! Appreciate helps!
The shortened body content hints on it being filled/rendered for 'teaser' view instead of 'full'. In what context do you issue this print statement?
EDIT: The node templates are usually used for both, teaser and full output, but the decision on what to use, as well as the population of the content entries in the node object happen outside of the node template files. Within the node template file, the variable $teaser will be TRUE, if the node is to be shown as a teaser.
So you need to check in what context your node template gets called, as you'll have to configure that context to render the node as 'full'. This could be in many places, depending on who is responsible to provide the nodes you want to theme, e.g. if the node template gets called from a view, you'll need to configure the view to use 'full page' output, if it comes from a module, you'll need to check with the module settings, etc...
In node.tpl.php try
<?php print $content ?>
However,
<?php print $node->content['body']['#value']; ?>
works for me as well.
To get control over your teaser length the master value is set with Post Settings.
(Length of trimmed posts)
To control this by node type try: http://drupal.org/project/teaserbytype
NOTE: Teasers are cached so you'll need to http://drupal.org/project/retease
However, if you want to just get it done in the node template you could run a node_load() and have everything... but that's not the best practice.
FYI: you can control what CCK fields show up in $content under Display Options.
PS: In teaser mode I often make use of truncate_utf8().

Conditional link to node within views?

I have two content types, book and chapter. Each chapter node contains a node reference of the book to which it belongs. I have created a view which displays the title of each chapter for a given book. Within the view, the title field has been configured to link to its node.
All works well. I am now interested in updating the view to not display a link to a chapter's node when the chapter's body is empty. Thus this view would display a list of chapter titles for a book and link only to those chapters that have a body.
Can this be accomplished using out of the box Views functionality? If not, how would something like this be accomplished?
Thanks.
I'd use the Views Custom Field module to implement your custom logic. It allows you to grab the data fetched by Views and manipulate it at will with PHP. Very handy.
I'm answering my own question because my response to ceejayoz is poorly formatted.
What I did to accomplish this was to first download and enable views_customfield. Second, I placed Title and Body fields within the view, both excluded from display. Third, within a Customfield: PHP code field I placed the following code:
<?php
if (strlen(trim($data->node_revisions_body)) == 0) {
return $data->node_title;
} else {
return l($data->node_title, drupal_get_path_alias('node/' . $data->nid));
}
?>
There's also this clever workaround which allows you to achieve this very easily:
Add 2 title fields one with link and one with no link, and make both
them exclude from display.
Add body field,
In No result behavior put title with no link replacement token to
it.
In Rewrite results behavior put title with link replacement token to
it.
Tick hide if empty.
Source

How do you remove the default title and body fields in a CCK generated Drupal content-type?

When you create a new content type in Drupal using the Content Creation Kit, you automatically get Title and Body fields in the generated form. Is there a way to remove them?
If you're not a developer (or you want to shortcut the development process), another possible solution is to utilize the auto_nodetitle module. Auto nodetitle will let you create rules for generating the title of the node. These can be programmatic rules, tokens that are replaced, or simply static text. Worth a look if nothing else.
To remove the body edit the type, expand "Submission form settings" and put in blank for body field label. For title you can rename it to another text field. If you really have no need for any text fields you can create a custom module, say called foo, and create function foo_form_alter() which replaces $form['title'] with a #value when $form['type']['#value'] is your node type.
No need to install anything:
when editing the content type, press "Edit"
(on the menu of Edit | Manage fields | Display fields )
click on the Submission form settings
on the Body field label:
Leave it blank, it would remove the Body field.
If you're not a developer (or you want
to shortcut the development process),
another possible solution is to
utilize the auto_nodetitle module.
Auto nodetitle will let you create
rules for generating the title of the
node. These can be programmatic rules,
tokens that are replaced, or simply
static text. Worth a look if nothing
else.
And to add on to William OConnor's solution...
The module is poorly documented unfortunately. It's really only effective if you use PHP with it in my opinion. Check off the "Evaluate PHP in Pattern" and type into the "Pattern for the title" field something like:
<?php echo $node->field_staff_email[0]['email']; ?>
or:
<?php echo $node->field_staff_name[0]['value'] . '-' . gmdate('YmdHis'); ?>
...where I had a field with an internal name of "field_staff_email" and was using the CCK Email module -- thus the 'email' type was used. Or, I had a field with an internal name of "field_staff_name" and was just an ordinary text field -- thus the 'value' type was used. The gmdate() call on the end is to ensure uniqueness because you may have two or more staff members named the same thing.
The way I discovered all this was by first experimenting with:
<?php print_r($node); ?>
...which of course gave crazy results, but at least I was able to parse the output and figure out how to use the $node object properly here.
Just note if you use either of these PHP routines, then you end up with the Content list in Drupal Admin showing entries exactly as you coded the PHP. This is why I didn't just use gmdate() alone because then it might be hard to find my record for editing.
Note also you might be able to use Base-36 conversion on gmdate() in order to reduce the size of the output because gmdate('YmdHis') is fairly long.
The initial answers are all good. Just as another idea for the title part... how about creating a custom template file for the cck node type. You would copy node.tpl.php to node-TYPE.tpl.php, and then edit the new file and remove where the title is rendered. (Dont forget to clear your cache).
Doing it this way means that every node still has a title, so for content management you aren't left with blank titles or anything like that.
HTH!

Resources