Avatar of bogorman
bogorman
 asked on

Preprocess function to modify title

I have created a view (called magazine_articles) which generates a list of titles and authors (based on a content type called Article).
When I group the fields by IssueDate, the block appears thus:

Jan 2010
Title        Author
Title        Author

Nov 2009
Title       Author

etc

The articles are in a bimonthly magazine which is published on our website.
 
What I want to do is to convert the group headings to:

Jan-Feb 2010
Nov-Dec 2010
Sep-Oct 2010

I have tried to write a preprocess function in my template.php file (in my theme directory):

function faith_home_preprocess_views_view__magazine_articles(&$variables) {
 
  $month = substr($variables['title'],0,3);
  $year = substr($variables['title'],4,4);
switch($month){
    case "Jan": $issuename="Jan-Feb"; break;
    case "Mar": $issuename="Mar-Apl"; break;
    case "May": $issuename="May-Jun"; break;
    case "Jul": $issuename="Jul-Aug"; break;
    case "Sep": $issuename="Sep-Oct"; break;
    case "Nov": $issuename="Nov-Dec"; break;
    default: $issuename = "None"; break;
}
$title = $issuename." ".$year;
 
 
 
  $variables['magtitle'] = $title;


}

I have modified the coding in my views-view-table--magazine_articles.tpl.php file to:

<?php if (!empty($magtitle)): ?>
  <<?php print $group_element; ?><?php print drupal_attributes($group_attributes); ?>>
    <?php print $magtitle; ?>
  </<?php print $group_element; ?>>

but no group headings are printed.

Can anyone tell me how I can solve this?
DrupalPHP

Avatar of undefined
Last Comment
bogorman

8/22/2022 - Mon
theremon

Hi

sorry if this is dump question, but in your function you're setting   $variables['magtitle'] = $title;
and then in your code you're calling variable $magtitle - not the array.
Is that correct?
bogorman

ASKER
Hi,
Thanks for your suggestion.
As I understand it the variables are, as you say, held in an array - variables$('name of variable') (or vars$('name of variable')  but to actually refer to them in the template.php file you use the variable name 'name of variable'
Is this correct?
By the way, the name of the template file I have created is:

semanticviews-view-unformatted--magazine-articles.tpl.php

as I am putting the view in a block and this is the suggested name which appears in views Theme Information. Anyway, I am fairly certain that this is the file that is being used as, when I add the function to my template.php file and change the variable name from $title (the default variable name for the group title) to $magtitle, the headings disappear. Mind you I suppose it is possible that the coding in the tpl.php file does not recognise $magtitle and does not call the coding in template.php

I am really confused about this. Have done my best to find the answer on the Drupal forums, where there is a lot of information about template files, but without success. If you, or anyone else, can suggest anything else, I would be grateful.
theremon

Hi again

that's not what I meant - I was actually asking why are you assigning a value to $variables["magtitle"] in your function, but are calling $magtitle in the other code instead of $variables["magtitle"]. Then I realized you where talking about Drupal - something I totally missed at first. My problem is that I know nothing about it.
In any case, can I suggest something? As you said, everything works fine when you use $title. Then why don't you use $variables['title']=$title in faith_home_preprocess_views_view__magazine_articles ?
As I said I don't know what Drupal needs in its coding, so it's just a wild guess here.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
bogorman

ASKER
Hi,
Thanks for your suggestion but unfortunately it does not seem to make any difference. Have tried changing the coding in my template.php file to:

function faith_home_preprocess_views_view__magazine_articles(&$vars, $hook) {
 
  $month = substr($vars['title'],0,3);
  $year = substr($vars['title'],4,4);
switch($month){
    case "Jan": $issuename="Jan-Feb"; break;
    case "Mar": $issuename="Mar-Apl"; break;
    case "May": $issuename="May-Jun"; break;
    case "Jul": $issuename="Jul-Aug"; break;
    case "Sep": $issuename="Sep-Oct"; break;
    case "Nov": $issuename="Nov-Dec"; break;
    default: $issuename = "None"; break;
}
$vars['title'] = $issuename." ".$year;
 
 
 
  $vars['magtitle'] = $vars['title'];

}

and in the tpl.php file:

 
?>
<?php if (!empty($vars('magtitle'))): ?>
  <<?php print $group_element; ?><?php print drupal_attributes($group_attributes); ?>>
    <?php print ($vars('magtitle')); ?>


but I get a white screen.

If I then change the coding in the tpl.php file back to :

?>
<?php if (!empty($magtitle)): ?>
  <<?php print $group_element; ?><?php print drupal_attributes($group_attributes); ?>>
    <?php print ($magtitle); ?>

the page appears but the group headings are missing.

I know nothing about debugging php but is there a way of displaying/printing on the screen values of variables in the template php file and in the tpl.php file?
sheep7

You could try this

function faith_home_preprocess_views_view__magazine_articles(&$vars){
  $month = substr($vars['title'],0,3);
  $year = substr($vars['title'],4,4);
  switch($month){
    case "Jan": $issuename="Jan-Feb"; break;
    case "Feb": $issuename="Jan-Feb"; break;
    case "Mar": $issuename="Mar-Apl"; break;
    case "Apr": $issuename="Mar-Apl"; break;
    case "May": $issuename="May-Jun"; break;
    case "Jul": $issuename="Jul-Aug"; break;
    case "Aug": $issuename="May-Jun"; break;
    case "Sep": $issuename="Sep-Oct"; break;
    case "Oct": $issuename="Sep-Oct"; break;
    case "Nov": $issuename="Nov-Dec"; break;
    case "Dec": $issuename="Nov-Dec"; break;
    default: $issuename = "None"; break;
  }
  $vars['title'] = $issuename." ".$year;
  $vars['magtitle'] = $vars['title'];
}

Open in new window

bogorman

ASKER
Hi,
Thanks very much for your suggestion. It still does not print the group headers.
Perhaps it might help you if I supply the coding for the two files. I attach them.

You will note that the template file is called

semanticviews-view-unformatted--magazine-articles.tpl.php

This is because I have used the Semantic Views style in views. This file name is suggested under Theme Information, Style Output.

1. semanticviews-view-unformatted--magazine-articles.tpl.php:


<?php
// $Id: semanticviews-view-unformatted.tpl.php,v 1.1.2.3 2009/09/19 22:33:48 bangpound Exp $
/**
 * @file views-view-unformatted.tpl.php
 * Default simple view template to display a list of rows.
 *
 * @ingroup views_templates
 */
 
  
 
 
?>
<?php if (!empty($magtitle)): ?>
  <<?php print $group_element; ?><?php print drupal_attributes($group_attributes); ?>>
    <?php print ($magtitle); ?>
  </<?php print $group_element; ?>>
<?php endif; ?>
<?php if (!empty($list_element)): ?>
  <<?php print $list_element; ?><?php print drupal_attributes($list_attributes); ?>>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
  <?php if (!empty($row_element)): ?>
  <<?php print $row_element; ?><?php print drupal_attributes($row_attributes[$id]); ?>>
  <?php endif; ?>
    <?php print $row; ?>
  <?php if (!empty($row_element)): ?>
  </<?php print $row_element; ?>>
  <?php endif; ?>
<?php endforeach; ?>
<?php if (!empty($list_element)): ?>
  </<?php print $list_element; ?>>
<?php endif; ?>

2. template.php: (just the function)

function faith_home_preprocess_views_view__magazine_articles(&$vars){
  $month = substr($vars['title'],0,3);
  $year = substr($vars['title'],4,4);
  switch($month){
    case "Jan": $issuename="Jan-Feb"; break;
    case "Feb": $issuename="Jan-Feb"; break;
    case "Mar": $issuename="Mar-Apl"; break;
    case "Apr": $issuename="Mar-Apl"; break;
    case "May": $issuename="May-Jun"; break;
    case "Jul": $issuename="Jul-Aug"; break;
    case "Aug": $issuename="May-Jun"; break;
    case "Sep": $issuename="Sep-Oct"; break;
    case "Oct": $issuename="Sep-Oct"; break;
    case "Nov": $issuename="Nov-Dec"; break;
    case "Dec": $issuename="Nov-Dec"; break;
    default: $issuename = "None"; break;
  }
  $vars['title'] = $issuename." ".$year;
  $vars['magtitle'] = $vars['title'];
}

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Thomas4019

What about just using this.

http://drupal.org/project/computed_field
bogorman

ASKER
Thanks for your suggestion. I have tried entering the following code under 'Computed Code' after adding a new (computed) field to the content type. However, I get an error ('an error occured...') when I try to preview the view. I am also wondering how, if I can cure this error, I can group by the date field and replace the group heading with this computed field.



$month = substr($node_field[0]['value'],0,3);
$year = substr($node_field[0]['value'],4,3);

switch($month){
    case "Jan": $month="Jan-Feb"; break;
    case "Feb": $month="Jan-Feb"; break;
    case "Mar": $month="Mar-Apl"; break;
    case "Apr": $month="Mar-Apl"; break;
    case "May": $month="May-Jun"; break;
    case "Jul": $month="Jul-Aug"; break;
    case "Aug": $month="May-Jun"; break;
    case "Sep": $month="Sep-Oct"; break;
    case "Oct": $month="Sep-Oct"; break;
    case "Nov": $month="Nov-Dec"; break;
    case "Dec": $month="Nov-Dec"; break;
    default: $month = "None"; break;
  }
$node_field[0]['value'] = $month .$year;


If you can give me more help on this, I would be most grateful, as it seems a very sensible suggestion.

PS. Hope I can reopen this question by posting this.
Thomas4019

You don't actually use $node_field. "$node_field" should be replaced with your fields name. If your field's name is field_month, then use $field_month instead. Otherwise drupal will have no idea which field you are talking about.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
bogorman

ASKER
The field which is used for grouping in the view is field_issue_date.

I have tried using the code:

$field_issue_date[0]['value']='Group Heading';

just to test it, and I get nothing appearing in the view after adding the new computed field Group Heading. I know I must be making an elementary mistake. Can you suggest a reason?

Thomas4019

Sorry, my mistake, I haven't used computed fields for a while. Put this in the "Computed Code" box.

$node_field[0]['value'] = 'Group Heading';

Then set views to group by your computed field value. Your earlier snippet wasn't working because i don't think computed field is allowed to modify values other than it's own.
bogorman

ASKER
I've tried that but it still doesn't work. Perhaps I have not explained the problem accurately.

I have defined a content type which contains fields:
Author
Title
IssueDate

This content type is to be used to format/display articles from a bimonthly magazine which we publish on our website.  The articles are grouped by the IssueDate which is always the first day of the (first) month of issue, i.e. If the issue is Mar-Apl 2009, the Issue date is 1/3/2009 (UK date).

I can group by this date and have set the display of the date to be Month/Year so the headings appear as

Jan 2010
Nov 2009
Sep 2009
(in reverse order)

What I want to do is to still group in this way but to change the headings to read

Jan-Feb 2010
Nov-Dec 2009
Sep-Oct 2009


Does this clarify it? I assume I would still have to group by the IssueDate field and use the computed field to display the issue description, if this is possible
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Thomas4019

OK, so i think i have finally been able to get what you are after, here's how i did it. In your content type, add a computed field called Grouped Date (you may have alreadly done this). Look at screenshot A for what it should look like. Then click "Manage Fields" for this content type and for Grouped Date click "Configure". Set it's computed code to be something like the code snippet I attached (you will likely need to modify the field names). Set the calculated fields "Data Length" to something around 30.

Then go to you view which you are working on. Add Content: Grouped Field to the views fields (see screenshot B). Make sure you set it to be excluded from the views display; this will make it not show up in the table but usable as the grouping field. (see screenshot C). Then under "Basic Settings" click "Table". Then click settings.  Change the "Grouping field:" to "Grouped Date" (see screenshot D).

I believe this should produce what you are looking for though i may have missed something. Finally I have attached Screenshot E which shows the final product I achieved.
$time = strtotime($node->field_issue_date[0]['value']);
$month = date('M',$time);

switch($month)
{
    case "Jan": $month="Jan-Feb"; break;
    case "Feb": $month="Jan-Feb"; break;
    case "Mar": $month="Mar-Apl"; break;
    case "Apr": $month="Mar-Apl"; break;
    case "May": $month="May-Jun"; break;
    case "Jul": $month="Jul-Aug"; break;
    case "Aug": $month="May-Jun"; break;
    case "Sep": $month="Sep-Oct"; break;
    case "Oct": $month="Sep-Oct"; break;
    case "Nov": $month="Nov-Dec"; break;
    case "Dec": $month="Nov-Dec"; break;
    default: $month = "None"; break;
}

$node_field[0]['value'] = $month.' '.date('Y',$time);

Open in new window

Capture3.PNG
Capture4.PNG
Capture2.PNG
Capture.PNG
Capture5.PNG
Thomas4019

My code snippet assumes you are using the Date CCK plugin for the issue date. If that's not the case, we will need to change the snippet.
bogorman

ASKER
Hi,
Thanks again for your most helpful advice. Unfortunately I am still getting an error when I try and preview the view.
I am wondering if it might be due to the date format. I see a number of formats under Date and Time including one which appears as Short date format (07/03/2010 - 11:43). The Issue Date field in my content type has an Input format shown as 07/03/2010 - 23:45:32 and a Custom input format as d-m-Y with Years back and forward as -8:+3 (I want to be able to enter dates back to 2002). Is this relevant?
Regarding the Computed Code. I have not modified it. The only difference between your and my fields is the name of the new field (yours is Grouped Date, mine is Group Heading). I assume this will not affect the coding.
One other query. Can you group by this new field? As the data in the field will take the form Jan-Feb 2010, can one group by this or should the grouping by the Issue Date field which is in date format?
Any further help will be gratefully received.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
bogorman

ASKER
Have tried to follow your instructions as closely as I can. I have even deleted the previously created Group Heading field and created a new one like yours called Grouped Date.
I no longer get an error when previewing the view but the preview just has "Grouped Date;" at the top of each page of the preview but the nodes are not actually grouped.
The other strange thing is that, if I change the coding in the computed field to:

$node_field[0]['value'] = "Test heading";

again, only the label "Grouped Date: " appears and that only appears once at the top of each preview page.
bogorman

ASKER
Have tried it again but it is still the same. One thing, however, I have noticed is that, if I set the format of the  Grouped Date field as Raw Text I get an error :

The page at http://localhost says:
Anerror occurred at /drupal/admin/build/views/ajax/preview/magazine_articles.

If I change the format to Computed Value, the error disappears but I only get a heading "Grouped Date :" with no data alongside it. and this heading appears only once at the top of each preview page.

One last point. If I change the Computed Code of Grouped Date to :

$node_field[0]['value'] = 'Test heading';

I still get no data in the heading, only its title.

The whole thing is very odd as it obviously works for you. If you can suggest anything else I would be most grateful.
Thomas4019

Sorry for the late response. For the computed values to change you must create a new node. From what i can tell old nodes are not retroactively updated. I think that might be causing part of your problem.

So try creating a new node and see if it works for that.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
bogorman

ASKER
Not quite sure what you mean. The view (or its page/block) displays all the Article nodes. How can I create a new node? (you can see I am not experienced with Drupal!)
bogorman

ASKER
I think you're right about creating the nodes. I now find that some of the nodes are grouped with a correct heading and others are not.
Do you mean that every time I change/add a computed field to a content type I have to re-create all the pages?  
Is it literally a case of deleting the nodes that don't appear and recreating them?
I have tried unpublishing a node and republishing it but it makes no difference.
There is a module called Node Export. Can I export the nodes (presumably to a text file) and then import them using Node Import?
bogorman

ASKER
I have investigated further and still cannot understand what is happening.
Have created a new node with the Article content type and it is not listed under the appropriate heading. Its date is 1/10/2010 but it appears in a group on the first page with no heading above it. Could it be that the computed field data is not being saved?  How can I check what is actually in the database?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
Thomas4019

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
bogorman

ASKER
Thanks, Thomas, for all the work you have done on this.
I still can't get it to work but don't feel it is right to delay assigning the points as you have been so helpful.
I think it may be due partly to the date format as you suggest.
Regards
Brian