diff --git a/modules/backend/formwidgets/RichEditor.php b/modules/backend/formwidgets/RichEditor.php index 6d53c01a5..fe0b33924 100644 --- a/modules/backend/formwidgets/RichEditor.php +++ b/modules/backend/formwidgets/RichEditor.php @@ -1,5 +1,7 @@ vars['value'] = $this->getLoadValue(); } + /** + * Returns a single collection of available page links. + * This implementation has room to place links under + * different groups based on the link type. + * @return array + */ public function onGetPageLinks() { - $links = [ - ['name' => 'Select a page...', 'url' => false], - ['name' => 'Some url', 'url' => 'some/url'], - ['name' => 'Other thing', 'url' => 'else/thing'], - ['name' => 'More', 'url' => 'more/thing'] - ]; + $links = []; + $types = $this->getPageLinkTypes(); + + $links[] = ['name' => 'Select a page...', 'url' => false]; + + $iterator = function($links, $level = 0) use (&$iterator) { + $result = []; + foreach ($links as $linkUrl => $link) { + + /* + * Remove scheme and host from URL + */ + $baseUrl = Request::getSchemeAndHttpHost(); + if (strpos($linkUrl, $baseUrl) === 0) { + $linkUrl = substr($linkUrl, strlen($baseUrl)); + } + + $linkName = str_repeat(' ', $level * 4); + $linkName .= is_array($link) ? array_get($link, 'title', '') : $link; + $result[] = ['name' => $linkName, 'url' => $linkUrl]; + + if (is_array($link)) { + $result = array_merge( + $result, + $iterator(array_get($link, 'links', []), $level + 1) + ); + } + } + + return $result; + }; + + foreach ($types as $typeCode => $typeName) { + $links = array_merge($links, $iterator($this->getPageLinks($typeCode))); + } return ['links' => $links]; } @@ -80,4 +117,48 @@ class RichEditor extends FormWidgetBase $this->addCss('css/richeditor.css', 'core'); $this->addJs('js/build-min.js', 'core'); } + + /** + * Returns a list of registered page link types. + * This is reserved functionality for separating the links by type. + * @return array Returns an array of registered page link types + */ + protected function getPageLinkTypes() + { + $result = []; + + $apiResult = Event::fire('backend.richeditor.listTypes'); + if (is_array($apiResult)) { + foreach ($apiResult as $typeList) { + if (!is_array($typeList)) { + continue; + } + + foreach ($typeList as $typeCode => $typeName) { + $result[$typeCode] = $typeName; + } + } + } + + return $result; + } + + protected function getPageLinks($type) + { + $result = []; + $apiResult = Event::fire('backend.richeditor.getTypeInfo', [$type]); + if (is_array($apiResult)) { + foreach ($apiResult as $typeInfo) { + if (!is_array($typeInfo)) { + continue; + } + + foreach ($typeInfo as $name => $value) { + $result[$name] = $value; + } + } + } + + return $result; + } } diff --git a/modules/backend/formwidgets/richeditor/assets/js/build-min.js b/modules/backend/formwidgets/richeditor/assets/js/build-min.js index 378ab12e1..c512bf0bd 100755 --- a/modules/backend/formwidgets/richeditor/assets/js/build-min.js +++ b/modules/backend/formwidgets/richeditor/assets/js/build-min.js @@ -1939,7 +1939,7 @@ else {return{init:function() {if(!this.opts.pageLinksHandler)return this.modal.addCallback('link',$.proxy(this.pagelinks.load,this))},load:function() -{return;var $select=$('') $('#redactor-modal-link-insert').prepend($select) this.pagelinks.storage={};this.$editor.request(this.opts.pageLinksHandler,{success:$.proxy(function(data){$.each(data.links,$.proxy(function(key,val){this.pagelinks.storage[key]=val $select.append($('