Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Inplace editable: Difference between revisions

From MoodleDocs
m (Protected "Inplace editable": Developer Docs Migration ([Edit=Allow only administrators] (indefinite)))
 
(24 intermediate revisions by 4 users not shown)
Line 1: Line 1:
inplace_editable is a mini-API being introduced under MDL-51802 for 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}}
 
== Implementing inplace_editable in a plugin ==
 
The best way is to explain the usage on a simple example. Imagine we have plugin '''tool_mytest''' that needs to implement in-place editing of a field 'name' from db table tool_mytest_mytable. We are going to call this itemtype "mytestname". This plugin only uses one in-place element so we will basically ignore $itemtype argument in the saver function.
 
Create a file '''/admin/tool/mytest/classes/inplace_editable_saver.php''' with a class:
<code php>
namespace tool_mytest;
class inplace_editable_saver implements core\inplace_editable_saver_base {
    public function render_value($itemtype, $record) {
        $value = $record->name;
        $displayvalue = format_string($value); // You can add html link to the displayvalue.
        $editable = has_capability('tool/mytest:update', context_system::instance());
        $edithint = 'Edit mytest name';
        $editlabel = 'New value for ' . format_string($value); // Obviously, use language strings here!
        return new \core\output\inplace_editable('tool_mytest', $itemtype, $record->id, $editable, $displayvalue,
            $value, $edithint, $editlabel);
    }
 
    public function update_value($itemtype, $itemid, $newvalue) {
        global $DB;
        // 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('tool/mytest:update', context_system::instance());
        $record = $DB->get_record('tool_mytest_mytable', array('id' => $itemid), '*', MUST_EXIST);
        // Update the record (the best way is to call an existing function for it).
        $DB->update_record('tool_mytest_mytable', array('id' => $itemid, 'name' => $newvalue));
        $record->name = $newvalue;
        return $this->render_value($itemtype, $record);
    }
}
</code>
 
Please note that function "render_value" is not part of the interface and you can choose to call rendering function differently or not have it at all. Although it is recommended because you need to use the same templateable/renderable element for actually displaying the element on the page. For example:
 
<code php>
$tmpl = new \tool_mytest\inplace_editable_saver();
echo $OUTPUT->render($tmpl->render_value('mytestname', $record));
</code>
 
== How does it work ==
 
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
 
== See also ==
 
[Category:AJAX]

Latest revision as of 12:38, 18 September 2023

Important:

This content of this page has been updated and migrated to the new Moodle Developer Resources. The information contained on the page should no longer be seen up-to-date.

Why not view this page on the new site and help us to migrate more content to the new site!