Every repository with this icon (
Every repository with this icon (
Anatomy of a Feather
It’ll be easier to just paste the entire source of the Text feather. Here you go:
<?php
class Text extends Feathers implements Feather {
public function __init() {
$this->setField(array("attr" => "title",
"type" => "text",
"label" => __("Title", "text"),
"optional" => true,
"bookmarklet" => "title"));
$this->setField(array("attr" => "body",
"type" => "text_block",
"label" => __("Body", "text"),
"preview" => true,
"bookmarklet" => "selection"));
$this->setFilter("body", "markup_post_text");
$this->setFilter("title", "markup_post_title");
}
public function submit() {
if (empty($_POST['body']))
error(__("Error"), __("Body can't be blank."));
fallback($_POST['slug'], sanitize($_POST['title']));
return Post::add(array("title" => $_POST['title'],
"body" => $_POST['body']),
$_POST['slug'],
Post::check_url($_POST['slug']));
}
public function update($post) {
if (empty($_POST['body']))
error(__("Error"), __("Body can't be blank."));
$post->update(array("title" => $_POST['title'],
"body" => $_POST['body']));
}
public function title($post) {
return oneof($post->title, $post->title_from_excerpt());
}
public function excerpt($post) {
return $post->body;
}
public function feed_content($post) {
return $post->body;
}
}
Much like Modules, Feathers are simply a class that extends the Feathers class. Feathers also implement the “Feather” interface, to ensure that all Feathers provide the correct structure.
Feather Functions
__init(), __install(), and __uninstall() are optional, but the rest are necessary.
__init()
__init() is exactly like the __construct() function, but it’s only called after all of the Modules and Feathers are instantiated. It exists because otherwise, calling other Triggers in __construct() would be problematic because not every Module/Feather is ready to react.
__install()
__install() is called when the Feather is enabled.
__uninstall($confirm = false)
__uninstall() is called when the Feather is disabled. There is one possible argument, and that’s if your Feather has a “confirm” YAML setting; the argument will be a boolean of whether or not they confirmed the dialogue.
submit()
This is the function called when submitting a post. Note that this controls the entirety of adding posts.
Tasks to be completed here:
- Create the post.
- Redirect to either the bookmarklet “Done!” page or to the post’s URL, depending on where they submitted from.
update($post)
This function handles updating the post. That is the only task.
title($post)
This should return the most logical title for the post. If there is no obvious title field, use $post->title_from_excerpt().
excerpt($post)
This returns the source for the excerpt. You do not need to do any truncation, it’s handled automatically by wherever it was called from.
feed_content($post)
This returns the content for the feed entry.
Feather Construct Functions
There are a few functions that the Feather class provides to Feathers, mainly to be used in the __construct() or __init() function.
“$this->setField()”:http://chyrp.net/docs/class/Feather-php.html#Feather.setField
This sets a field for your Feather, for use on Write/Edit pages.
“$this->setFilter()”:http://chyrp.net/docs/class/Feather-php.html#Feather.setFilter
This function is used for applying a filter to a given attribute:
function __construct() {
$this→setFilter(“body”, “markup_post_text”);
}
“$this->customFilter()”:http://chyrp.net/docs/class/Feather-php.html#Feather.customFilter
Much like like setFilter, but the function passed as the second argument is a function your Feather provides.
“$this->respondTo()”:http://chyrp.net/docs/class/Feather-php.html#Feather.respondTo
Calling this sets your Feather up to respond to a Trigger like a Module would. See: Triggers






