Creating a page that displays a set of objects

Permalink 1 user found helpful
Hi there,

I am new to concrete5 and I have a very basic question. I already tried finding an answer in forums for hours but had no luck. I have a lot of experience with other CMS and have also programming skills but this issue is causing me quite a headache.

I want to achieve the following result:

There are objects/content types, let's say fruits. Each of these objects has some attributes like name, image, color, taste.
That means that I have to define somewhere in concrete5 that there is the fruit content type and that this content type can have the attributes name, image, color and taste.

Then, I want to display all fruits that have been added on one page with the appropriate attributes.

First I thought this can be done by blocks, but then I figured out that this is not the right way. Then I thought this can be achieved by defining a "fruit page" type and adding a new page for each fruit. But I don't need a separate page for each fruit. I just need one page that displays all fruit objects. Then I thought that attribute sets are what I am looking for but it seems that that is again something else.
All I want is:
- Possibility to add fruit objects
- Display all fruit objects on a page

Maybe I am thinking to complicate but this has to be a really straight forward thing...

I would really appreciate any help.

 
drbiskit replied on at Permalink Best Answer Reply 1 Attachment
drbiskit
You need to look at the 'Page List' block, by creating a custom template for this, you can easily achieve what you want. This block is one of the key features of building dynamic sites with C5, and once you get your head around it, it's pretty powerful.

As the name suggests, a 'Page list' block will list out other pages on a page. By default, you can configure it to show specific lists depending on your requirements (such as e.g. location within site, or by 'page type'), and order them (by e.g. sitemap order / date / etc).

By creating a custom template, you can then take things much further, and start using custom attribute, etc.

So, in your case, you would create a page type for 'fruit', and then create each of your fruit objects as an individual page with this new page type.
(More about page typeshttp://www.concrete5.org/documentation/general-topics/page-types/)...

Then you need to create some custom attributes for:
image ('Image/File' attribute - give this a handle of 'fruit_image')
color ('Text' attribute - give this a handle of 'fruit_color')
taste ('Text' attribute - give this a handle of 'fruit_taste')

(More about attributes:http://www.concrete5.org/documentation/general-topics/attributes/)...

You don't really need to create one for 'name', you can just use the page title for this (unless they need to different for any reason)

Then add the attached file to your site here:
root/blocks/page_list/templates/fruit_list.php (Change the file extension of the attached file from 'txt' to 'php')

This is a really basic custom page template file that just deals specifically with returning your data based on the attributes added. Have a look at the default page list file below if you would like to see all default possible options (such as e.g. paging and rss etc):
root/concrete/blocks/page_list/view.php - I have stripped a lot of this stuff out of the custom page template file to help show the logic/requirements here.

Once this is done, simply add a page list block and filter by 'pages of type' - your 'fruit' page type should now be one of the options. This should return all of your fruit objects on one page.

Once you have the list showing on your page, but still in edit mode, click on the page list, and select 'Custom template'. There will be a 'Custom template' select control, select 'Fruit list', and save.
(More about custom templates:http://www.concrete5.org/documentation/using-concrete5/in-page-edit...

You should now have your list with your attributes showing. You will need to amend the html in the fruit list file to suit. I haven't actually tested it, so if it doesn't work first time, let me know!

Hope I've covered everything, and that this helps.

===============
Code for custom template:

<?php 
defined('C5_EXECUTE') or die(_("Access Denied."));
$th = Loader::helper("text"); 
$ih = Loader::helper('image');
$pg = Page::getCurrentPage();
$pgID = $pg->getCollectionID();
?>
<ul class="fruit-list">
<?php  foreach ($pages as $page):
      // Prepare data for each page being listed...
      $title = $th->entities($page->getCollectionName());
      $url = $nh->getLinkToCollection($page);
      $color = $page->getAttribute('fruit_color');
      $taste = $page->getAttribute('fruit_taste');
      $img = $page->getAttribute('fruit_image');
developer2k replied on at Permalink Reply
Wow! Thank you very much. This works out of the box. I have never seen such a fast and good support before.

Also it seems that concrete5 is a pretty easy system for such dynamic custom content. This is a clear advantage over other cms.

Best regards
bbeng89 replied on at Permalink Reply
bbeng89
This is definitely an informative post, but he specifically mentioned that each fruit doesn't need to be its own page.

I think you would probably be better off checking out the Custom Objects Demo here -http://www.concrete5.org/marketplace/addons/custom-objects-demo/....

This will show you how you can manage custom objects (fruits) from the dashboard and then list them on the front end. Basically exactly your requirements. It's a long tutorial but very informative.
drbiskit replied on at Permalink Reply
drbiskit
Yep, good point. Actually I haven't seen this demo before so thanks for sharing.
From my point of view, using page lists is just a way of sorting/listing out content. Just because you are creating a page in the system doesn't mean that you have to actually show it on the front end site. You can just pull out the attributes in a page list, and so I believe it's a very flexible and valid solution to this problem.
bbeng89 replied on at Permalink Reply
bbeng89
Yeah, pages and page lists are definitely convenient but if you are never going to be showing the page then you are adding a lot of unnecessary overhead. Writing custom objects is much more lightweight, especially when you're involving page attributes. I could be wrong, but I believe every time you retrieve a page attribute you are adding another DB query. So that adds up.

I will agree that using pages and page lists would be a much quicker way of implementing what OP is trying to do, but personally I would just go with custom objects because it's more lightweight and specific to the problem at hand. Also I think it would be more intuitive from a user standpoint to add/edit objects from a dashboard management page than trying to use the sitemap or composer. Just my opinion though.
drbiskit replied on at Permalink Reply
drbiskit
Yeah I take your point there. If you have a lot of data to manage, then using custom objects would probably be a tidier and more efficient way of doing things. I guess it just depends on your requirements/constraints/etc. As usual, it's good to know as many options as possible! Cheers.
developer2k replied on at Permalink Reply
i think I will go with the solution with the page list and page types, because the list will not grow much and MAYBE in the future there will be the need to show the objects on separate pages too. It is also quick in development.

Also, I found an add-on that seems pretty nicehttp://www.concrete5.org/marketplace/addons/designer-content...
Maybe I can do something with this tool
drbiskit replied on at Permalink Reply
drbiskit
You can accomplish many great things with that add-on - it's one of the the best add-ons in the marketplace, and it's free! Super useful, but I'm not entirely sure you'll be able to do what you want with it out of the box. Regardless, it is an amazing tool for building blocks, and often a brilliant starting point when developing.
hereNT replied on at Permalink Reply
hereNT
What I've found VERY useful in some apps is to use both custom objects and pages.

The built in page list creates really crazy huge queries and can get really slow for large lists.

What you can do instead, is when you create your custom object and list from the dashboard page you make is to also create a page somewhere hidden. Then you can use the attributes on that page, even outputting their forms directly in the custom object edit form. You can also validate those attributes before saving and updating the page object.

When you list / filter / sort the custom object, you can join to the pageSearchIndex table (think that's it) and do much, much simpler and faster queries. Put a 'getPageObject()' function on your custom object, and then you have the faster listing interface, but still all of the functionality of a page.
JohntheFish replied on at Permalink Reply
JohntheFish
That sounds useful enough to be written up as a howto.