From dcc130c7cf5ffec22aa25ab44b6174bfcb3b7b8a Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Fri, 6 Aug 2021 12:00:50 +0100 Subject: [PATCH] MDL-72289 output: allow customised inplace editable icon for editing. Add optional argument to allow it to be defined by caller, and set the default icon for `select` type as appropriate. --- lib/classes/output/inplace_editable.php | 18 +++++++++++++++++- lib/external/externallib.php | 5 +++++ lib/outputcomponents.php | 2 +- lib/templates/inplace_editable.mustache | 4 +++- lib/upgrade.txt | 2 ++ 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/classes/output/inplace_editable.php b/lib/classes/output/inplace_editable.php index bcfa2f8e47e..27cd1822edf 100644 --- a/lib/classes/output/inplace_editable.php +++ b/lib/classes/output/inplace_editable.php @@ -28,6 +28,7 @@ namespace core\output; use templatable; use renderable; use lang_string; +use pix_icon; /** * Class allowing to quick edit a title inline @@ -89,6 +90,11 @@ class inplace_editable implements templatable, renderable { */ protected $edithint = null; + /** + * @var pix_icon icon to use to toggle editing + */ + protected $editicon = null; + /** * @var bool indicates if the current user is allowed to edit this element - set in constructor after permissions are checked */ @@ -117,9 +123,10 @@ class inplace_editable implements templatable, renderable { * @param string $value what needs to be edited - usually raw value from the database, it may contain multilang tags * @param lang_string|string $edithint hint (title) that will be displayed under the edit link * @param lang_string|string $editlabel label for the input element in the editing mode (for screenreaders) + * @param pix_icon|null $editicon icon to use to toggle editing */ public function __construct($component, $itemtype, $itemid, $editable, - $displayvalue, $value = null, $edithint = null, $editlabel = null) { + $displayvalue, $value = null, $edithint = null, $editlabel = null, ?pix_icon $editicon = null) { $this->component = $component; $this->itemtype = $itemtype; $this->itemid = $itemid; @@ -128,6 +135,7 @@ class inplace_editable implements templatable, renderable { $this->value = $value; $this->edithint = $edithint; $this->editlabel = $editlabel; + $this->editicon = $editicon; } /** @@ -190,6 +198,9 @@ class inplace_editable implements templatable, renderable { if ($this->displayvalue === null) { $this->displayvalue = $options[$this->value]; } + if ($this->editicon === null) { + $this->editicon = new pix_icon('t/expanded', (string) $this->edithint); + } return $this; } @@ -242,6 +253,10 @@ class inplace_editable implements templatable, renderable { ); } + if ($this->editicon === null) { + $this->editicon = new pix_icon('t/editstring', (string) $this->edithint); + } + return array( 'component' => $this->component, 'itemtype' => $this->itemtype, @@ -250,6 +265,7 @@ class inplace_editable implements templatable, renderable { 'value' => (string)$this->value, 'edithint' => (string)$this->edithint, 'editlabel' => (string)$this->editlabel, + 'editicon' => $this->editicon->export_for_pix(), 'type' => $this->type, 'options' => $this->options, 'linkeverything' => $this->get_linkeverything() ? 1 : 0, diff --git a/lib/external/externallib.php b/lib/external/externallib.php index 7bc8ab6b56a..b9345666b43 100644 --- a/lib/external/externallib.php +++ b/lib/external/externallib.php @@ -498,6 +498,11 @@ class core_external extends external_api { 'itemid' => new external_value(PARAM_RAW, 'identifier of the updated item', VALUE_OPTIONAL), 'edithint' => new external_value(PARAM_NOTAGS, 'hint for editing element', VALUE_OPTIONAL), 'editlabel' => new external_value(PARAM_RAW, 'label for editing element', VALUE_OPTIONAL), + 'editicon' => new external_single_structure([ + 'key' => new external_value(PARAM_RAW, 'Edit icon key', VALUE_OPTIONAL), + 'component' => new external_value(PARAM_COMPONENT, 'Edit icon component', VALUE_OPTIONAL), + 'title' => new external_value(PARAM_NOTAGS, 'Edit icon title', VALUE_OPTIONAL), + ]), 'type' => new external_value(PARAM_ALPHA, 'type of the element (text, toggle, select)', VALUE_OPTIONAL), 'options' => new external_value(PARAM_RAW, 'options of the element, format depends on type', VALUE_OPTIONAL), 'linkeverything' => new external_value(PARAM_INT, 'Should everything be wrapped in the edit link or link displayed separately', VALUE_OPTIONAL), diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php index 599b479fbe3..ffac6b56bac 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -736,7 +736,7 @@ class pix_icon implements renderable, templatable { return [ 'key' => $this->pix, 'component' => $this->component, - 'title' => $title + 'title' => (string) $title, ]; } } diff --git a/lib/templates/inplace_editable.mustache b/lib/templates/inplace_editable.mustache index cfdf87c0085..b7582ed96f9 100644 --- a/lib/templates/inplace_editable.mustache +++ b/lib/templates/inplace_editable.mustache @@ -56,7 +56,9 @@ {{^ linkeverything }}{{{displayvalue}}}{{/ linkeverything }} {{# linkeverything }}{{{displayvalue}}}{{/ linkeverything }} - {{#pix}}t/editstring,core,{{{edithint}}}{{/pix}} + + {{#editicon}}{{#pix}}{{key}}, {{component}}, {{title}}{{/pix}}{{/editicon}} + {{#js}} diff --git a/lib/upgrade.txt b/lib/upgrade.txt index b827f050ef3..c81067ee277 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -63,6 +63,8 @@ information provided here is intended especially for developers. * New method \core_user::awaiting_action() has been introduced to check if the user is fully ready to use the site or whether there is an action (such as filling the missing profile field, changing password or agreeing to the site policy) needed. +* The inplace_editable element constructor now accepts an optional pix_icon parameter to use as it's editing icon when + rendered. The default icon for "select" types has also changed to a dropdown caret ("t/expanded"). === 3.11.2 === * For security reasons, filelib has been updated so all requests now use emulated redirects.