See Release Notes
Long Term Support Release
<?php // This file is part of Moodle - http://moodle.org/ // // Moodle is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Moodle is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with Moodle. If not, see <http://www.gnu.org/licenses/>.< /** < * This is the external method used for fetching the addable blocks in a given page. < * < * @package core_block < * @since Moodle 3.11 < * @copyright 2020 Mihail Geshoski <mihail@moodle.com> < * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later < */ <namespace core_block\external;< defined('MOODLE_INTERNAL') || die(); < < global $CFG; < require_once($CFG->libdir . '/externallib.php'); < < use external_api; < use external_function_parameters; < use external_multiple_structure; < use external_single_structure; < use external_value;> use core_external\external_api; > use core_external\external_function_parameters; > use core_external\external_multiple_structure; > use core_external\external_single_structure; > use core_external\external_value;/** * This is the external method used for fetching the addable blocks in a given page. *> * @package core_block * @copyright 2020 Mihail Geshoski <mihail@moodle.com> > * @since Moodle 3.11* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class fetch_addable_blocks extends external_api { /** * Describes the parameters for execute. * * @return external_function_parameters */ public static function execute_parameters(): external_function_parameters { return new external_function_parameters( [ 'pagecontextid' => new external_value(PARAM_INT, 'The context ID of the page.'), 'pagetype' => new external_value(PARAM_ALPHANUMEXT, 'The type of the page.'), 'pagelayout' => new external_value(PARAM_ALPHA, 'The layout of the page.'), 'subpage' => new external_value(PARAM_TEXT, 'The subpage identifier', VALUE_DEFAULT, ''),> 'pagehash' => new external_value(PARAM_ALPHANUMEXT, 'Page hash', VALUE_DEFAULT, ''),] ); } /** * Fetch the addable blocks in a given page. * * @param int $pagecontextid The context ID of the page * @param string $pagetype The type of the page * @param string $pagelayout The layout of the page * @param string $subpage The subpage identifier> * @param string $pagehash Page hash that can be provided instead of all parameters above* @return array The blocks list */< public static function execute(int $pagecontextid, string $pagetype, string $pagelayout, string $subpage = ''): array {> public static function execute(int $pagecontextid, string $pagetype, string $pagelayout, > string $subpage = '', string $pagehash = ''): array {global $PAGE; $params = self::validate_parameters(self::execute_parameters(), [ 'pagecontextid' => $pagecontextid, 'pagetype' => $pagetype, 'pagelayout' => $pagelayout, 'subpage' => $subpage,> 'pagehash' => $pagehash,] );> if ($params['pagehash']) { $context = \context::instance_by_id($params['pagecontextid']); > // If pagehash is specified, all other parameters are ignored, all information // Validate the context. This will also set the context in $PAGE. > // about the page is stored in the session. self::validate_context($context); > > $page = \moodle_page::retrieve_edited_page($params['pagehash'], MUST_EXIST); // We need to manually set the page layout and page type. > self::validate_context($page->context); $PAGE->set_pagelayout($params['pagelayout']); > } else { $PAGE->set_pagetype($params['pagetype']); > // For backward-compatibility and Mobile App instead of pagehash $PAGE->set_subpage($params['subpage']); > // we can specify context, pagelayout, pagetype and subtype. >// Firstly, we need to load all currently existing page blocks to later determine which blocks are addable.> $page = $PAGE; $PAGE->blocks->load_blocks(false); > }< $PAGE->blocks->load_blocks(false); < $PAGE->blocks->create_all_block_instances();> $page->blocks->load_blocks(false); > $page->blocks->create_all_block_instances();< $addableblocks = $PAGE->blocks->get_addable_blocks();> $addableblocks = $page->blocks->get_addable_blocks();< return array_map(function($block) {> return array_map(function($block) use ($page) { > $classname = \block_manager::get_block_edit_form_class($block->name);return [ 'name' => $block->name,< 'title' => get_string('pluginname', "block_{$block->name}")> 'title' => get_string('pluginname', "block_{$block->name}"), > 'blockform' => $classname::display_form_when_adding() ? $classname : null,]; }, $addableblocks); } /** * Describes the execute return value. * * @return external_multiple_structure */ public static function execute_returns(): external_multiple_structure { return new external_multiple_structure( new external_single_structure( [ 'name' => new external_value(PARAM_PLUGIN, 'The name of the block.'), 'title' => new external_value(PARAM_RAW, 'The title of the block.'),> 'blockform' => new external_value(PARAM_RAW, ] > 'If this block type has a form when it is being added then the classname of the form')), 'List of addable blocks in a given page.' ); } }