Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core\output\action_menu;
18
 
19
use core\output\action_menu;
20
use core\output\action_link;
21
use core\output\pix_icon;
22
use core\output\renderable;
23
use core\output\renderer_base;
24
use moodle_url;
25
use stdClass;
26
 
27
/**
28
 * An action menu action
29
 *
30
 * @package core
31
 * @category output
32
 * @copyright 2013 Sam Hemelryk
33
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class link extends action_link implements renderable {
36
    /**
37
     * True if this is a primary action. False if not.
38
     * @var bool
39
     */
40
    public $primary = true;
41
 
42
    /**
43
     * The action menu this link has been added to.
44
     * @var action_menu
45
     */
46
    public $actionmenu = null;
47
 
48
    /**
49
     * The number of instances of this action menu link (and its subclasses).
50
     *
51
     * @var int
52
     * @deprecated since Moodle 4.4.
53
     */
54
    protected static $instance = 1;
55
 
56
    /**
57
     * Constructs the object.
58
     *
59
     * @param moodle_url $url The URL for the action.
60
     * @param pix_icon|null $icon The icon to represent the action.
61
     * @param string $text The text to represent the action.
62
     * @param bool $primary Whether this is a primary action or not.
63
     * @param array $attributes Any attribtues associated with the action.
64
     */
65
    public function __construct(moodle_url $url, ?pix_icon $icon, $text, $primary = true, array $attributes = []) {
66
        parent::__construct($url, $text, null, $attributes, $icon);
67
        $this->primary = (bool)$primary;
68
        $this->add_class('menu-action');
69
        $this->attributes['role'] = 'menuitem';
70
    }
71
 
72
    /**
73
     * Export for template.
74
     *
75
     * @param renderer_base $output The renderer.
76
     * @return stdClass
77
     */
78
    public function export_for_template(renderer_base $output) {
79
        $data = parent::export_for_template($output);
80
 
81
        // Ignore what the parent did with the attributes, except for ID and class.
82
        $data->attributes = [];
83
        $attributes = $this->attributes;
84
        unset($attributes['id']);
85
        unset($attributes['class']);
86
 
87
        // Handle text being a renderable.
88
        if ($this->text instanceof renderable) {
89
            $data->text = $this->render($this->text);
90
        }
91
 
92
        $data->showtext = (!$this->icon || $this->primary === false);
93
 
94
        $data->icon = null;
95
        if ($this->icon) {
96
            $icon = $this->icon;
97
            if ($this->primary || !$this->actionmenu->will_be_enhanced()) {
98
                $attributes['title'] = $data->text;
99
            }
100
            $data->icon = $icon ? $icon->export_for_pix() : null;
101
        }
102
 
103
        $data->disabled = !empty($attributes['disabled']);
104
        unset($attributes['disabled']);
105
 
106
        $data->attributes = array_map(function ($key, $value) {
107
            return [
108
                'name' => $key,
109
                'value' => $value,
110
            ];
111
        }, array_keys($attributes), $attributes);
112
 
113
        return $data;
114
    }
115
}
116
 
117
// Alias this class to the old name.
118
// This file will be autoloaded by the legacyclasses autoload system.
119
// In future all uses of this class will be corrected and the legacy references will be removed.
120
class_alias(link::class, \action_menu_link::class);