Modifying the Generated News Pages and NewsList

Permalink Browser Info Environment
I was looking for a way to create an "Announcements" page that was easy and free, and I landed on EasyNews (http://www.concrete5.org/marketplace/addons/easy-news/). It was both easy and free...but it didn't work exactly as I wanted it to. Being a tinkerer, I dove in and made some changes. Hopefully some of this will help people out who want to do similar things.

First of all, here are the goals I wanted to accomplish.

Out of the box, the NewsList includes a title that's a link to the actual news item and the summary text. I wanted it to also include a time stamp and place a horizontal rule between news items to make things more readable.

On the news pages themselves, I wanted each page to have some header text, with a title and a link back to the news list, separated from the news content by a horizontal rule. I used a hard-coded, constant title for all my news pages but it shouldn't be too much of a leap to change that to the headline of the news story. I've attached a screenprint of what my header looks like.

I wanted the header to be added automatically, and I wanted to still be able to edit it properly from the dashboard.

So the first thing I changed was the NewsList. I found the view.php file for it in <my web root>/packages/easy_news/blocks/easynews_list. There's a php "for" code block that iterates through $cArray (the page's collection array). I added code under the $title assignment, changing this:

$title = $cobj->getCollectionName(); ?>
<h3 class="ccm-page-list-title"><a href="<?php  echo $nh->getLinkToCollection($cobj)?>"><?php  echo $title?></a></h3>


with this:

$title = $cobj->getCollectionName(); ?>
<span style="font-size: x-small;"><?php echo strftime("%A, %b %e, %Y at %r", strtotime($cobj->getCollectionDatePublic())); ?></span>
<h3 class="ccm-page-list-title"><a href="<?php  echo $nh->getLinkToCollection($cobj)?>"><?php  echo $title?></a></h3>


This is all within a div block at the top of the file, and just before the div is closed I put this:

<hr />
</div>


That's it. The second attachment shows what my news list items now look like.

Now the hard part: making EasyNews create the news page the way I want it without hosing up the edit/delete functionality. (Actually, it wasn't that hard.)

First, to create the page with the header I wanted, I had to edit the controller.php file in <my web root>/packages/easy_news/controllers/dashboard/easy_news. The page creation happens in the saveData function, which looks like this out-of-the-box:

private function saveData($p) {
      $blocks = $p->getBlocks('Main');
      foreach($blocks as $b) {
         $b->deleteBlock();
      }
      $bt = BlockType::getByHandle('content');
      $data = array('content' => $this->post('newsBody'));
      $p->addBlock($bt, 'Main', $data);
      Loader::model("attribute/categories/collection");
   }


Fairly straightforward, it runs through all the blocks in the Main section of page $p and deletes them. It then creates a content block and populates it with the news body. I just copied the three lines that create the newsBody block and modified them to include my text. Where I hard-coded in "NinjaNews," you could easily plop in $this->post('title'). My new function looks like this:

private function saveData($p) {
      $blocks = $p->getBlocks('Main');
      foreach($blocks as $b) {
         $b->deleteBlock();
      }
                // Add title and back-link
      $bt = BlockType::getByHandle('html');
      $data = array('content' => '<table width="100%" border="0"><tr valign="top"><td align="left"><h1>NinjaNews Report</h1></td><td align="right"><a href="/index.php/team-ninja/">Back</a></td></tr></table><hr />');
           $p->addBlock($bt, 'Main', $data);
      $bt = BlockType::getByHandle('content');
      $data = array('content' => $this->post('newsBody'));
      $p->addBlock($bt, 'Main', $data);
      Loader::model("attribute/categories/collection");
   }


Note that I made the block an HTML block. This made my life a lot easier later when I wanted the edit/delete tasks to work. Since we're talking about that, let's visit the dashboard view.php file located in <my web root>/packages/easy_news/single_pages/dashboardeasy_news. I first made the above mods to the saveData function by adding my header as a content block. But then I ran into a problem when trying to edit or delete my news item because of this line in the dashboard view file:

$eb = $news->getBlocks('Main');
if (is_object($eb[0])) $newsBody = $eb[0]->getInstance()->getContent();


As you can see, the code simply grabs the first block in the Main area and populates the rich content edit box with its contents. So what was happening was that the header text (NinjaNews, etc.) was showing up - which wasn't cool if I wanted to edit the newsBody. So I surmised I could just change that to $eb[1] instead of $eb[0] - which would have worked. But I decided instead to just make sure that the newsBody was the only content block, and make anything else I add an HTML block. This way I can tweak things later to add essentially anything I want without having to mess with the code again.

Just making the extra block an HTML block wasn't enough. I had to add code to the view.php file to make sure that any non-content blocks in the Main area of the page were ignored. I did that by changing the above-quoted lines to the following:

$eb = $news->getBlocks('Main');
   // Cycle through Main blocks to find the Content block.
      foreach($eb as $_b) {
      if (is_object($_b)) {
         if ($_b->getBlockTypeHandle() != 'content') continue;
         $newsBody = $_b->getInstance()->getContent();
      }
   }


Going through this process has helped increase my understanding of many aspects of concrete5, and has opened me to some possibilities. One day I may even attempt my own creation of an add-on block. Certainly there's a good chance that I'll go back and change "NinjaNews" to the news item's title. For now, though, I hope some of this has helped. Happy concreting! :)

~artninja

2 Attachments

Type: Discussion
Status: New
artninja
View Replies:
reachdigital replied on at Permalink Reply 1 Attachment
reachdigital
His this is great!

What would be actual code to get it to have the back button below the article and have the title as the actual article title. Also could the back button be an image.

Or how could we add a breadcrum above the title? This makes it easy to navigate.

Sorry I'm no coder.

I have attached a sample
hanicker replied on at Permalink Reply
hanicker
Hi, just answered your question in the other thread at
http://www.concrete5.org/index.php?cID!4781&editmode...

concrete5 Environment Information

Browser User-Agent String

Hide Post Content

This will replace the post content with the message: "Content has been removed by an Administrator"

Hide Content

Request Refund

You have not specified a license for this support ticket. You must have a valid license assigned to a support ticket to request a refund.