How to theme views fields in Drupal 7 correctly - performance

I need to theme views in Drupal 7. There is a content type 'Book' and I need to list 5 books and theme them in special manner(preview image, title and author).
When I override views-view-field.tpl.php and print raw SQL result, I see that all fields are displayed. This code
echo "<pre>";
print_r($row);
echo "</pre>";
gives
[entity] => stdClass Object
(
[title] => ...
....
[nid] => 34
...
[body] => Array
...
But I don't want pass [body] from database to php side, because it can be huge and cause a performance issue. I haven't selected [body] in view settings.
Is there a way to pass only certain fields to views-view-field.tpl.php?
Thanks in advance.

The variables available are written in the documentation inside the sites/all/modules/views/theme folder's files.
Usually, the variable you need to look at and modify on a views-view-fields.tpl.php template is $fields
I use the devel module (http://drupal.org/project/devel) to view the variables available:
<?php
//after enabling the devel module...
dpm($fields);
// This will print a Kuomo display on the page with the array's vars
?>
Generally, on a view of nodes,
<?php print $fields['title']->content; ?>
will print the node title. For fields, try
<?php print $fields['field_FIELDNAME']->content; ?>
If you have the memory, you can capture ALL vars available on the template in the Kuomo with
<?php dpm(get_defined_vars()); ?>
Make sure you clear your cache before you try to view the vars.

If what you want to do is theme a certain field you can create a template for that specific field like this one: views-view-field--field-nameofmyfield.tpl.php place it in your theme folder and rescan the templates in the Theme:information part of the View configuration.
For that to work you have to have the field added to Fields in the View.

To sort through your information in a theme use this:
<?php dpm ($rows); ?> // View all the information in the view
<?php foreach ($rows as $row_count => $row): ?>
<?php print $row['title'];
<?php print $row['nid'];
<?php endforeach; ?>

If you want to change of theme of view then Change views-view-fields.tpl.php like this:
<div class="pagecontent">
<div class="colleft">
<?php if($fields['field_file']->content){ ?><div class="views-field-file"><?php print $fields['field_file']->content; ?></div><?php } ?>
</div>
<div class="colright">
<div class="views-field-title"><?php print $fields['title']->content; ?></div>
<div class="views-field-body"><?php print $fields['body']->content; ?></div>
<div class="views-field-view-node"><?php print $fields['view_node']->content; ?></div>
</div>
</div>

Related

Joomla : render custom fields in search results

Is it possible to display core custom fields in search results ? I can add custom fields in template override for article view like this :
<?php echo $this->item->jcfields[1]->value; ?>
It works fine for com_content but it doesn't display anything in search results (com_search).
Is there a way to render my custom fields in the search results ?
Thank you.
You can create a template overwrite for the com search component like other overwrites by creating a 'html/com_content/com_search/search' folder in your current template.
In this you copy the content of /components/com_search/views/search/tmpl
You can find a description with examples here: https://docs.joomla.org/Customising_the_Smart_Search_results_page
This is targeted at Smart Search (com_finder), however you can follow this by replacing com_finder for com_search.
Then you customize and add the custom fields in the file: default_results.php in your overwrite file.
Please note that the custom fields are a string in the $result object
["jcfields"]=>
string(26) "Custom Field,Search123,1,2"
echo "My fields: ".$result->jcfields;
If you want to do this for Smart Search in Joomla 4, here is the solution.
Create a template override of the file: components/com_finder/tmpl/search/default_result.php
At the start of the file (after line 20), add this code:
// Creates an array with all the CF of the current article.
$articleCustomFields = Joomla\Component\Fields\Administrator\Helper\FieldsHelper::getFields('com_content.article', $this->result, true);
// Creates an associative array using the custom field id as a key.
$articleCustomFields = \Joomla\Utilities\ArrayHelper::pivot($articleCustomFields, 'id');
Then print the custom fields in the page:
<ul class="fields-container">
<?php
// We are using the custom fields with ids 10 and 3
if (!empty($articleCustomFields[10]->value)) : ?>
<li class="field-entry">
<span class="field-label"><?= $articleCustomFields[10]->label ?>:</span>
<span class="field-value"><?= $articleCustomFields[10]->value; ?></span>
</li>
<?php
endif; ?>
<?php
if (!empty($articleCustomFields[3]->value)) : ?>
<li class="field-entry">
<span class="field-label"><?= $articleCustomFields[3]->label ?>:</span>
<span class="field-value"><?= $articleCustomFields[3]->value; ?></span>
</li>
<?php
endif; ?>
</ul>
If you are looking for a complete tutorial, please have a look at:
https://blue-coder.com/help/blog/adding-custom-fields-in-the-search-results

Changing error delimiter to empty in codeigniter

i want to change the form error wrapper. I don't want any wrapper no div no <p> i have used this code <?php echo form_error('inv_data_val','<div>','</div>'); ?> it do adds the div but when i used <?php echo form_error('inv_data_val','',''); ?> it still shows p
http://www.codeigniter.com/userguide3/libraries/form_validation.html#changing-the-error-delimiters
$this->form_validation->set_error_delimiters('', '');
In your controller.

Magento: Switch From Grid view To List View, Without Changing URL

I was wondering if you could guide me how to allow the user to select either list or grid view, without changing the URL of the catalog/category page.
I.e., the page is either www.example.com/category?mode=grid OR www.example.com/category?mode=list but I want to make it just www.example.com/category and show the grid view by default, with the list view being displayed without changing the URL.
I hope you can help
There is no tutorial I guess..you have to do your own code ..And its not a big deal .. Open your list.phtml file in app/design/frontend/default/YOURTHEME/template/catalog/product/
Here you can see, they separate two view mode like this,
<div class="category-products">
<?php echo $this->getToolbarHtml() ?>
<?php // List mode ?>
<?php if($this->getMode()!='grid'): ?>
<?php $_iterator = 0; ?>
<ol class="products-list" id="products-list">
<?php foreach ($_productCollection as $_product): ?>
..bla.. bla ...
And Grid mode:
<?php else: ?>
<?php // Grid Mode ?>
Here they check the mode like this
<?php if($this->getMode()!='grid'): ?>
Just remove this condition, so that you can load both views, so now just add new css class or id to separate both modes, and manage them by Js like onclik event or something like that ...

Image type as background in drupal-7

Hi everybody: i had define a new content type that have:
a title, a link field, and a image field.
So, i want to make that image comes to be the background of the content type when i show it. How can i do that?
Thanks in advance
A very quick way to do this would be to add a custom node.tpl.php to your theme and do this:
<?php
$url = file_create_url($node->field_image[$node->language][0]['uri']);
?>
<div style="background:url(<?php echo $url; ?>) left top no-repeat;" id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>
// Rest of node.tpl.php here
I wanted to comment on Clive's answer, but do not have the "rep" :(. Wouldn't it be best with a check for node-type?
<?php
if ($type == 'MACHINE_NAME_FOR_YOUR_TYPE') {
$url = file_create_url($node->field_image[$node->language][0]['uri']);
?>
<div style="background:url(<?php echo $url; ?>) left top no-repeat;" id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>
<?php } ?>
// Rest of node.tpl.php here
If you use Field Group module, check Field Group Background Image.
Steps:
Go to the Manage Display page of your content type
Create a div (background image) field group as a wrapper of the node
Select the image field you want to use. Optionally, you can specify an image style
Do not output your image field

How to implement a sidebar in Zend Framework

How do I implement a sidebar in Zend Framework?
I know that I can use a placeholder or something similar in Zend_layout, but how do I automatically generate the code for the sidebar in my controllers without having to call a sidebar class within every controller?
My setup is as follows
Application
- modules
- blog
- other modules
I only want the sidebar for my blog module.
I have found this http://www.zfforums.com/zend-framework-components-13/model-view-controller-mvc-21/how-layout-sidebar-etc-2677.html but I do not understand the last part "just inject your layout, register it with the front controller ..."
You could just have a action and view in one of your controllers which renders the sidebar.
from the layout for the blog module you just call:
<? echo $this->action('action','controller','module',array('optionalparams'=>1); ?>
on the position where you want to have it. So one call to one action.
Rather than use the action stack and the action() view helper, you could render a "partial view script" that includes your sidebar elements.
# in your layout.phtml
<div id="sidebar">
<?php echo $this->render('blog/_sidebar.phtml'); /*relative to your view scripts directory*/ ?>
</div>
# in blog/_sidebar.phtml
<div id="blog_categories">
<?php foreach ($this->categories as $category): ?>
<?php echo $category->name; ?>
<?php endforeach; ?>
</div>
The render() view helper is used to render the content of another view script. It has the same scope as all your other view scripts, so if there are any variable assigned to the view, they will be available to your partial. So in the example above, the categories variable was set in the controller.
There is another view helper called the partial() view helper. This function is a little more expensive since it creates its own variable scope. In other words, none of your current view variables will be available. You will have a clean slate to work with, which means you must pass in any variables you need:
# in your layout.phtml
<div id="sidebar">
<?php echo $this->partial('blog/_sidebar.phtml', array('categories2'=>$this->categories)); ?>
</div>
# in blog/_sidebar.phtml
<div id="blog_categories">
<?php foreach ($this->categories2 as $category): ?>
<?php echo $category->name; ?>
<?php endforeach; ?>
</div>
I don't find myself using partial() very often since it is more expensive, and I rarely need to create a separate context.
As far as setting up the variables for use in the sidebar partial ($this->categories in this example), I have used a number of different methods depending on the particular problem. If it's specific to a controller action, I will write the code and assign it in the view script:
# controller
public function somethingAction()
{
$this->view->categories = $this->_getCategoriesForThisParticularAction();
// other controller code
}
If my code is more generic to all the actions of the controller, I will utilize the controller's preDispatch() function. If it's more generic to multiple controllers, I will put the code in the init() of my base controller (a controller the most of my controllers extend).
Sometimes I do not even put the code in my controller. If it's simple enough, I just stick the code in the partial. If it's a little more complex, I will move it to a view helper. This may break the MVC pattern, but I think it really depends on the particular case in order to determine the best placement.
If you are using Zend_Layout, just add the sidebar with the Action viewhelper as Rufinus said.
in your layout script:
<div id="sidebar">
<?php echo $this->action('action', 'controller', 'module', array('optionalparams'=>1)); ?>
</div>
<div id="content">
<?php echo $this->layout()->content; ?>
</div>
This should meet the requirements posted in your question.

Resources