Jump to content

Templates Select (FieldtypeTemplates)


Hani

Recommended Posts

I needed a field that would allow me to select one or more templates, very much like the Page Select. So I created a module that does just that! I based it off of Ryan's FieldtypeModules because the functionality is quite similar. You can also define which templates are selectable by the asmselect field. Hope this helps someone that wants to do the same thing!

FieldtypeTemplates.module

  • Like 6
Link to comment
Share on other sites

  • 7 months later...

Hi,

I have used this Fieldtype but I have noticed a strange behaviour.

Also.

The field was used in a page and I have selected three values.

They are saved correct.
Now I delete two values and they are deleted and the page saved correctly.

When I try to delete the last value and to save the page, the last value is not deleted.

Where can be the reason?
Is there any limitation with this module?

Thanks in advance

Raul

Link to comment
Share on other sites

That's very strange behavior.  I haven't seen that before.  There shouldn't be any limitation since this is a simple module that extends a core module.

What version of ProcessWire are you using? I'm not sure if anything changed between 2.2 and 2.3 that would have resulted in this anomaly.

Link to comment
Share on other sites

  • 3 months later...

Hi, I got the same problem as Raul. Was just about to create my own thread with this same issue when I chanced across Raul's post. I'm using version 2.3 dev version..

to duplicate the issue:

1. Create a new field with type "Templates"

2. Add the field to a page.

3. Edit the page and select some templates.

4. Save the page.

5. Now, unselect all the templates.

6. Save the page.

You should see the templates get selected back again. You can unselect the templates and remove all but one. that's also the issue that Raul described above.

  • Like 1
Link to comment
Share on other sites

I just tested it in 2.3 and you're right.  I was able to duplicate the issue, pogidude.  I also was able to confirm that the problem exists in the Modules Select plugin created by Ryan.  I'll see if I can fix it - but offhand, Ryan - any idea why that would be happening?

  • Like 1
Link to comment
Share on other sites

I just tested it in 2.3 and you're right.  I was able to duplicate the issue, pogidude.  I also was able to confirm that the problem exists in the Modules Select plugin created by Ryan.  I'll see if I can fix it - but offhand, Ryan - any idea why that would be happening?

I was able to duplicate it too. Looks like it's a core issue when dealing with multi-value Fieldtypes that only have an array value (rather than an object value). These two modules (FieldtypeModules and FieldtypeTemplates) I think are the only two that would be affected. I've implemented a fix in the dev branch and will be testing it for a bit before committing. 

  • Like 1
Link to comment
Share on other sites

by the way, where is this array value (or object) checked in core?

The problem was in /wire/core/Inputfield.php. Here is the patch I'm testing if you are interested. I even had a comment in there to myself about a potential problem (see the TODO). 

--- a/wire/core/Inputfield.php
+++ b/wire/core/Inputfield.php
@@ -418,11 +418,15 @@ abstract class Inputfield extends WireData implements Module {
         */
        public function ___processInput(WireInputData $input) {

-               // if value was unset in the array, then just return
-               // TODO should this set an empty value rather than leaving an existing value?
-               if(!isset($input[$this->name])) return $this; 
+               if(isset($input[$this->name])) {
+                       $value = $input[$this->name]; 
+
+               } else if($this instanceof InputfieldHasArrayValue) {
+                       $value = array();
+               } else {
+                       $value = $input[$this->name];
+               }

-               $value = $input[$this->name]; 
 
Link to comment
Share on other sites

I'm getting this warning due to my usage of FieldtypeTemplates in my module:

PHP Warning:  Invalid argument supplied for foreach() in /srv/www/ssd.pw/html/site/modules/FieldtypeTemplates.module on line 96, referer: http://ssd.pw/admin/page/

Unfortunately, if $config->debug = true, then, when I go to http://ssd.pw/admin/page/ (which lists the page tree), all I get is the right arrow and now tree.

Here is the module I'm working on https://github.com/ryannmicua/ProcessContentFlow and this is the part where I'm calling the templates fieldtype https://github.com/ryannmicua/ProcessContentFlow/blob/master/PageContentFlow.module#L61

EDIT:

This is how I'm using the field in my module:

if(count($page->template_field)){
....

EDIT_END

The warning only appears if I haven't selected any template in the inputfield yet - like right after putting the template field to a fieldgroup and this field hasn't been set yet.

As a temporary fix, I added the following code on top of the code in FieldtypeTemplates::___formatValue()

if(empty($value)) return $this->getBlankValue($page, $field);

By the way, why no github?

Link to comment
Share on other sites

So, I added a FieldtypeTemplates to my template and set data to it like so:

if(isset($_POST['templates'])){
   //comma separated list of template ids
   $templates = explode(',', $_POST['templates']);
}

$mypage->the_templates_field = $templates;
$mypage->save();

and this resulted in an unset templates select field - like the field was newly added.

Took me awhile to figure it out but in FieldtypeTemplates::sanitizeValue() line #43:

foreach ($value as $k => $v) {
    // we allow integers
    if (is_int($v) && $this->templates->get($v))
        continue;

    // we allow templates
    if ($v instanceof Template)
        continue;

    // but we don't allow anything else
    unset($value[$k]);
}

I changed is_int($v) to is_numeric($v) as noted here http://php.net/manual/en/function.is-int.php and got the desired behaviour.

  • Like 1
Link to comment
Share on other sites

Unfortunately, if $config->debug = true, then, when I go to http://ssd.pw/admin/page/ (which lists the page tree), all I get is the right arrow and now tree.

That usually means there is a PHP error getting shown, which is preventing jQuery from parsing the JSON returned by ProcessPageList. The best way to see the error is to go to your JS browser tools and click on the "network" tab, then examine the request–you should see an error message followed by some JSON. 

Link to comment
Share on other sites

Actually, it's a warning I'm getting

PHP Warning:  Invalid argument supplied for foreach() in /srv/www/ssd.pw/html/site/modules/FieldtypeTemplates.module on line 96, referer: http://ssd.pw/admin/page/

so the problem only shows up if I have $config->debug = true. And this only happens when the Templates Select Field hasn't been set yet and I'm calling the field in my code already like this:

if(count($page->templates_field)){
....
Link to comment
Share on other sites

  • 3 weeks later...
  • 3 months later...

It's an asm select. You can select templates with it.

Say you create a field with the name select_template (FieldtypeTemplates)

Then in a page using a template with this field, you can select multiple templates. ( stored in a Wirearray ) 

// prepare templates for a selector string
$tpl = $page->select_template ? "template=" . implode("|",$page->select_template) : '';

// Search selector to search for pages with template(s) you selected in the page,
// max 5 pages, sorted on creation date, newest firsts.
$template_pages = $pages->find("sort=-created, limit=5, $tpl");

ps, not tested

Link to comment
Share on other sites

ah, ok, got it. thanks.

so this way i will get all pages with the selected template/s.

i thought it could be used as kind of templateblock in a page, which would be populated with certain content from the page.

this way the editor could choose a special layout for a part (e.g. hero) of the page without messing up the code.

Link to comment
Share on other sites

so this way i will get all pages with the selected templatei thought it could be used as kind of templateblock in a page, which would be populated with certain content from the page.

this way the editor could choose a special layout for a part (e.g. hero) of the page without messing up the code.

Martijn is right - it's not as straightforward as it sounds.  I've tried to do that before and the solution I came up with was simply to use child pages.  Since each part of a page's content can look different, it may contain different content.  While a repeater COULD be used, it doesn't give as much flexibility.  It all really depends on what you're trying to do.

Link to comment
Share on other sites

  • 3 months later...
  • 4 months later...

We receive the following warning:

This module does not indicate compatibility with this version of ProcessWire. It may still work, but you may want to check with the module author.

Could you please check the compatibility for 2.4.0 and the GIT Version
and update it to the current Version - Thanks

and it is als not possible to open the zip Error: Unable to open ZIP file, error code: 19

Link to comment
Share on other sites

lisandi,

Firstly, welcome to the forums.

This module seems to be an unusual case given that the link to the zip is not to a Github master.zip, but rather a forum attachment. I'll mention the issue to Ryan, but in the meantime, you can simply download the module from the first post in this thread. It should be fine compatibility-wise with PW 2.4, although perhaps we need to revisit the issue that @isellsoap mentions just above.

EDIT: I can confirm that the bug that prevents removal of all selected templates is still there in PW 2.4.12

Link to comment
Share on other sites

Hani, are you able to update this module listing to point to a GitHub repo of this project? With the module installation options in the PW modules menu, the current linked file (a .module file) doesn't work for automatic installation. While a GitHub repo is preferable, it may work if you can ZIP the module into an archive and post that instead. In either case, we need to update your module listing so that it points to GitHub and/or your module repo. Let me know if I can assist.

  • 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
×
×
  • Create New...