Jump to content

Help with widgets


thomas.isberg
 Share

Recommended Posts

Hi,

this PW looks like great stuff! Great work you guys!

I'm trying to find a solution for implementing widgets on my pages. For starters, I'm trying to create a simple widget which just requires two InputfieldText instances in the admin.

Now, I realized that I could create a template for the widget, and then add widgets to a page by creating child pages based on that template. I could then check for child pages using that widget template, and render those into my page instead of linking to them (as with normal child pages).

That works technically, but I don't like to idea that the list of child pages becomes a mix of actual child pages and in-page widgets.

How would you guys implement this feature? Would you use templates for widgets, and somehow alter the way the child pages are listed in the admin? Is there a way to present the widget child pages in a separate tab within the page editing view (and not display them in the Pages view, and not in the Children tab)?

Or would you create a Fieldtype which can hold multiple values, and an Inputfield which presents multible GUI elements? I've spent quite a lot of time trying this approach, but I can't get the values from the two InputfieldText:s to be sent when saving... I managed to read values into them from the DB though... However, that was achieved without extending the InputfieldWrapper, and I'm guessing that some important PW features went missing along the way then...

Any advice?

Cheers & thanks,

.Thomas

Link to comment
Share on other sites

Thomas, welcome to the forums and thanks for your feedback. I'm having some trouble with context here as 'widgets' is a generic term that can mean a lot of things. But if I understand correctly, you probably want to locate your widgets pages elsewhere, perhaps under a /widgets/ section in the site. Page references allow you to easily break outside of the parent/child relationship of the tree for the times when you don't want items in the tree.

You mentioned a fieldtype too, and I think that's always a good way to go. If you want a good working example to start from, grab the FieldtypeMapMarker, which comes with all the components you'd need and is commented and ready to modify towards your needs. Though I should mention this is a regular Fieldtype rather than a FieldtypeMulti (repeatable elements).

I'm also thinking that the FieldtypeRepeater that I'm working on may be helpful in your case. With this fieldtype, you specify a template and then it creates a repeater using the fields in that template. You can then add/delete/sort the items (groups of fields) within the page as much as you want. But this new fieldtype is only a work in progress, so may be another week or two till I've got a release-able beta version to play with.

If you can give me some more background on what widgets are in this context, I may have some more ideas too. I'm sure we can find a good solution.

Link to comment
Share on other sites

You can also put all the widgets under a 'widgets' parent page, and then choose the ones you want for each page with the page fieldtype http://processwire.c...page-fieldtype/

like this they will be separated from the children of each page, and you can use the same in several pages

edit:

may be another week or two till I've got a release-able beta version to play with.

Great news Ryan :)

Link to comment
Share on other sites

Thank you Ryan!

Haha, no "widgets" doesn't really provide the details... :)

Hm, after rephrasing this reply a couple of times... Here's what I want to achieve:

Create different blocks of content (widgets) which can be added to a page. Each page can have zero, one or several blocks. In my example the list of blocks will populate a column to the right of all other content on the page. The blocks will all share the same width, and each block will be placed underneath the previous block on the page. Kind of like a banner area on any website with advertising. This does not affect the PW solution though - a block could be presented anywhere on the page. That'll be up to the template.

These blocks can contain different types of content. One might auto generate a list of the latest blog posts. That one could either be completely automatic, but it could also require the administrator to define which blog to generate the content from, if the site contains several blogs. In that case a list of available blogs should be presented in the admin console. Another block could present x number of products on sale, with image and some text describing each product. In that case the admin console should present a list of available products, and allow the aministrator to add as many products as desired.

Ideally these blocks should be re-usable on any page. The products on sale should be the same on all pages where that block is used. However, as a starting point I would be happy just to achieve the possibility of adding blocks of content. If the administrator has to put the same content in several places, so be it. :). The benefit of using blocks instead of simple template content then, is that the administrator can change parts of the page to display something completely different, without changing the template and the fields required by that template.

Now, the examples above (blog posts and products on sale) are for background. What I need help with are these parts:

1. Creating the functionality to add as many blocks as desired to a page. I really want the administrator to manage these blocks from the page edit view. A FieldtypeMulti in a separate tag is probably the way to go.

2. Say that I want to create a block which puts two lines of text on the page. The admin console should simply present two InputfieldText instances. Now, I'd rally like to use existing Inputfields in the block's Inputfield, because then I can tap into their functionality, and the possibilities should be endless - a block could easily contain just about anything.

Oops, gotta run! I'll check in again as soon as I can and read through this reply. Don't know if what I wrote make sense... :)

Cheers & thanks!

.Thomas

Link to comment
Share on other sites

Thomas, thanks for the explanation, I think that I understand now. Diogo beat me to the reply, as I was going to suggest exactly what he said because that's the way I usually handle this need in PW sites. I suggest using asmSelect for your block selection inputfield, as that will enable you to both select the blocks and drag-sort them in the order you want them to appear.

Lets say that your page reference field is called 'blocks'. Each item in there refers to a page, and each of those 'block' pages has a template file that renders the block the way you want it output. The template file for your page that shows the blocks can output them like this:

foreach($page->blocks as $block) echo $block->render(); 

So the above would display all of your blocks selected on the page, and in the order the admin dragged them.

Link to comment
Share on other sites

  • 1 year later...

having issue with Page field and asmSelect input rendering a page template:

$widgets = $page->side_bar_widgets;
    foreach ($widgets as $widget) {
    $name = $widget->name;
    $widegtRender = $pages->get('name=$name'); // this is where im having trouble. it errors even thought the variable echos a name value
   echo $widegtRender->render();

}

i've tried get('name='$name); and get('name=\$name'); but errors everytime - Unknown Selector operator: '=$'

Any help would be great, thank you.

Link to comment
Share on other sites

having issue with Page field and asmSelect input rendering a page template:

$widgets = $page->side_bar_widgets;

    foreach ($widgets as $widget) {

    $name = $widget->name;

    $widegtRender = $pages->get('name=$name'); // this is where im having trouble. it errors even thought the variable echos a name value

   echo $widegtRender->render();

}

i've tried get('name='$name); and get('name=\$name'); but errors everytime - Unknown Selector operator: '=$'

Any help would be great, thank you.

ok, sorry totally worked it out and was easier than expected (like most things in processwire).

$widgets = $page->side_bar_widgets;
foreach ($widgets as $widget) {
    $name = $widget->name;
    echo $widget->render();
}
 
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...