|
|
(29 intermediate revisions by 4 users not shown) |
Line 1: |
Line 1: |
| inplace_editable is a mini-API introduced in Moodle 3.1. It allows developers easily add in-place editing of a value on any page. The interface looks very similar to activity name editing but implemented as AMD module using JQuery and is re-usable.
| | {{Template:Migrated|newDocId=/docs/apis/subsystems/output/inplace}} |
| | |
| == inplace_editable consists of ==
| |
| | |
| * Template core/inplace_editable
| |
| * Javascript module core/inplace_editable
| |
| * Webservice core_update_inplace_editable available from AJAX
| |
| * Interface core\inplace_editable_saver_base
| |
| | |
| == Implementing inplace_editable in a plugin ==
| |
| | |
| Define class \yourpluginname\inplace_editable_saver, typically it will look like this:
| |
| <pre>
| |
| namespace yourpluginname;
| |
| class inplace_editable_saver implements core\inplace_editable_saver_base {
| |
| public function render_value($itemtype, $object) {
| |
| $value = $object->name;
| |
| $displayvalue = format_string($value); // You can add html link to the displayvalue.
| |
| $editable = has_capability('....', context_system::instance());
| |
| $edithint = 'Edit name';
| |
| $editlabel = 'New value for '.format_string($value); // Obviously, use strings here!
| |
| return new \core\output\inplace_editable('yourpluginname', $itemtype, $object->id, $editable, $displayvalue,
| |
| $value, $edithint, $editlabel);
| |
| }
| |
| | |
| public function update_value($itemtype, $itemid, $newvalue) {
| |
| // Always clean and validate input!
| |
| $newvalue = clean_param($newvalue, PARAM_NOTAGS);
| |
| // Check permission of the user to update this item. Call require_login($course) if needed.
| |
| require_capability('....', context_system::instance());
| |
| $object = $DB->get_record('tablename', array('id' => $itemid), '*', MUST_EXIST);
| |
| // Update the record (the best way is to call an existing function for it).
| |
| $DB->update_record('tablename', array('id' => $itemid, 'name' => $newvalue));
| |
| return $this->render_value($itemtype, $object);
| |
| }
| |
| } | |
| </pre>
| |
| | |
| In your renderer where you display the value call:
| |
| | |
| <pre>
| |
| $tmpl = new core_tag\inplace_editable_saver();
| |
| echo $OUTPUT->render($tmpl->render_value($itemtype, $object));
| |
| </pre>
| |
| | |
| That's it!
| |
| | |
| == See also ==
| |
| | |
| [Category:AJAX]
| |