Using the $content variable in Drupal - drupal

While in page.tpl.php, what's the difference between the two?
render($page['content']);
render($content);
Or am I just looking at this the wrong way?

Drupal 6 used $content to display the body content, and Drupal 7 prefers $page['content'].
See the API documentation for page.tpl.php (Drupal 6) and page.tpl.php (Drupal 7).
I'd stick with the preferred method for the version you're using to ensure you get the expected results.

I think that is the same, because $content is in the array $page['content'].
Also, do print_r($page['content']); to see whats going on

It is printing the label because you have not hidden the label in the content fields admin area. To hide it, go to structure -> content types -> manage display -> label (hidden).

Related

how can i display contact us page on front page drupal 8?

I am creating scratch theme in drupal 8. Problem is occurred now i want to show contact us block display front page in footer right corner. I had used contact form module but it opens new page. I also used Contact block but result is nothing my concern is that how to does this?
If any one knows, tell me i do not have more knowledge about drupal thanks.
You can use this:-
$block = module_invoke('module_name', 'block_view', 'block_delta');
print render($block['content']);
You define a path to your form admin/content/webform.
Then you define your home page in Site Information admin/config/system/site-information.
More easy, contact block + twig tweak module, get the frontpage node theme sugestion, in my case node 2, -> node--2--full.html.twig and theming it.
content.field_my_field for fields and
drupal_block('mymachinenamebock') -> drupal_block('contactblock') got by the twig tweaks module
You can include the contact form to the footer region by modifying your theme's your_theme_name.theme file.

Place an Edit Button on the view node page in Drupal 7

I don't use the Drupal Tabs because they interfere with my CSS but I need the functionality of the Edit tab to be on that screen so that a user can edit the node after reviewing it.
Any ideas on how to do this? Functions? tpl placement? Thanks!
You can do this in a custom module as follows.
In yourcustommodule.module you implement hook_preprocess_node(). In there you check if the user has permissions to edit the node and you set the edit link.
function yourcustommodule_preprocess_node(&$vars) {
if (node_access("update", $vars['node']) === TRUE) {
$vars['edit_link']['#markup'] = l(t('Edit'), 'node/' . $vars['nid'] . '/edit');
}
}
In the node.tpl.php template in the theme you print the edit link if it is available.
<?php if (isset($edit_link)) : ?>
<p><?php print render($edit_link); ?></p>
<?php endif; ?>
If you do not have a node.tpl.php template in your theme folder than copy the one from modules/node.
If you're using the Views Format "Fields", one of the fields that you can add is "Edit Link." It's pretty flexible; it will tell you what text to display in the link. That's probably the preferred option.
If you're not using the "Fields" format, it gets trickier, especially since you're already interfering with some basic drupal styling. I'd need more information about your View and your skill set to recommend a method that doesn't cause more problems.
As a sidenote: I learned Drupal theming from the outside in, and used to use CSS that would interfere with the underlying drupal mechanics like tabs and contextual links. I've moved away from that; I find very few cases where I need to interfere with native styling-- and for those I can use custom .tpl's to get around.
EDIT: Ah. If you're not using views, a custom page .tpl is probably the best way to go. If you're not familiar, the structure for any node edit link is '/node/<NID>/edit' (for clean URL's) or '/?q=node/<NID>/edit' for old-style URL's. Depending on how your path aliases are set up, '/<url-alias>/edit' may work as well but the previous ones are more reliable.
This link on drupal.org gives a few options.
I think u can write a theme file(.tpl) for u specific case and theme page in whichever way u want

Customise output of $content in Drupal nodes

I have a custom content type called 'business-profile' and I have the template files 'node-business_profile.tpl.php' which works fine, but I want to be able to edit the layout of the $content variable
ie I want display some elements in particular divs etc
What is the best approach?
Assuming this is drupal 6, use the devel and theamer modules to show you the templates and how they can be overridden.
In short most of the variables in content can be overridden with custom field templates.

Help with Drupal Search Form Display

When displaying the search form in my header, what is the preferred method to use if it cannot fit into my header region??
should I...
create a custom region?
use some kind of 'print $search_form'
drupal_get_form()??
use the theme() function??
please help! I'm new to Drupal and trying to figure out the best 'Drupal Way' of doing things.
Depends on how you want to define or maintain the search form. If you think of it as part of the theme (that is, it's as static as the page background or colors), consider just using the following in page.tpl.php:
<?php if ($search_box): ?>
<?php print $search_box ?>
<?php endif; ?>
This is de-facto the Drupal Way: if your theme didn't modify the page variables, you get $search_box for free in page.tpl.php. Adding the conditional also lets site maintainers turn it off in the theme settings and specify permissions for it.
If you want to give site maintainers the ability to move it around from region to region, consider using the Search block. This way it can be utilized just as any other block on your site. This would also be considered Drupal Way-ish: you get the block for free if you enable the Search module.
If you want theme the forms, override search-theme-form.tpl.php if you use the first method, and search-box-form.tpl.php if you used the second. Both templates can be found in modules/search.
You could do any of those. You could create a custom region, and use CSS to change the display. You could alter the form with preprocessing hooks or hook_form_alter.
Depends on whatever is easiest for you.

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().

Resources