Skip to content

Instantly share code, notes, and snippets.

Created December 8, 2016 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/45cd44af1976fa9fcb2b4e0ac6a93fc1 to your computer and use it in GitHub Desktop.
Save anonymous/45cd44af1976fa9fcb2b4e0ac6a93fc1 to your computer and use it in GitHub Desktop.
class BlogPost_Controller extends Page_Controller
{
private static $allowed_actions = array (
'FilterOnTags'
);
public function init(){
parent::init();
}
public function FilterOnTags() {
$tagFilter = new DropdownField(
'TagFilter',
"So what'll it be, yes or no?",
array( "N"=>"No", "Y"=>"Yes")
);
$fields = new FieldList($tagFilter);
$actions = new FieldList('FilterFormTags', 'Post');
return new Form($this, 'FilterOnTags', $fields);
}
}
@NightJar
Copy link

NightJar commented Dec 8, 2016

Good day all
I have a question. Which would be actually quite simple I guess
I want to generate a dropdownfield: https://gist.github.com/anonymous/45cd44af1976fa9fcb2b4e0ac6a93fc1. As far as I know (quite new still) I would in the template just call $FilterOnTags right?
Which piece of the puzzel am i missing?

If you ever come back to view this gist again...

  • One small omission you're missing $actions from the Form constructor: Form::create($this, __FUNCTION__, $fields, $actions), but it should still render without actions I think.
  • You've named the class BlogPost_Controller which could be causing a conflict for the auto-loader, resulting in your method simply not existing.
  • Or it's because your template is simply in the wrong scope.

Expanding on that last point, you're declaring this on BlogPost_Controller, and thus is only in scope when you're rendering a BlogPost (directly, as a page) in your template. At a guess I'd say you're probably trying to render this on a Blog scope, which is fine, but you should define the method on Blog_Controller instead. You should be access it in a template on a BlogPost context with $Parent.FilterOnTags.

Furthermore (on the central point), if this isn't a pseudo-code snippit, and you're using the blog module, and you're not editing the module core code (which one shouldn't do), then you need to change your classname & inheritance. The manifest (for the autoloader) will choose one of the two declarations, it's possible this isn't working because this method simply doesn't exist in the included class.

To this end, you should look at the documentation for Extension (and DataExtension) and apply this method through their use. In that case it's a trivial change to change the class definition to class FilterTagExtension extends Extension and the form declaration to Form::create($this, $this->owner, $fields, $actions). Then applying with the config system (yml) to both Blog and BlogPost.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment