0

I'm fairly new to moodle and php in general. I created a new block which acts like a display for custom events and reminder. Inside that block is a button that should open the page of another plugin I created when clicked. But I can't manage to set the button with a link that opens the new page. When I press the button, nothing happens. If I enter the address of the page of the other plugin in the address bar, it opens as expected.

    global $CFG;
    global $DB;
    $mform = $this->_form;

    $reminderinfo='';
    $reminder = $DB->get_records('reminder_events');
    foreach ($reminder as $remind){
        $reminderinfo .= $remind->eventname . '  ' . $remind->time . '<br>';
    }

    $manageurl = $CFG->wwwroot . '/local/reminder/manage.php';
    // Add reminderinfo to mform
    $mform->addElement('html', '<input type="button" class="btn btn-primary" value="Add Reminder" onclick=\"location.href="'.$manageurl.'"\">');

Image of Inspector mode: enter image description here

Scorch
  • 153
  • 2
  • 17

2 Answers2

1

here's some tips

Never use literal language strings, always store the string so it can be translated. Not just for different languages but also different versions of a language, eg US English and British English.

In /local/reminder/lang/en/local_reminder.php add this

defined('MOODLE_INTERNAL') || die();

$string['addreminder'] = 'Add Reminder';

Then use the language string with

get_string('addreminder', 'local_reminder');

Don't use $CFG->wwwroot directly for creating urls.

$manageurl = $CFG->wwwroot . '/local/reminder/manage.php';

Use this instead

$manageurl = new \moodle_url('/local/reminder/manage.php');

Then you have 2 options.

Either create a link that is styled to look like a button and use the target attribute to open in a new window

$managelink = \html_writer::link($manageurl, get_string('addreminder', 'local_reminder'),
    ['class' => 'btn btn-primary', 'target' => '_blank']);

Or use the button renderer with the target attribute

$button = $OUTPUT->single_button($manageurl, get_string('addreminder', 'local_reminder'),
    'get', false, ['target' => '_blank']);

And a final tip

It's unusual to open a link in a new window in Moodle. This is because of accessibility. So I wouldn't recommend it.

See https://www.w3.org/TR/WCAG20-TECHS/G200.html

In general, it is better not to open new windows and tabs since they can be disorienting for people, especially people who have difficulty perceiving visual content.

Russell England
  • 9,436
  • 1
  • 27
  • 41
0

---update---

'<input type="button" class="btn btn-primary" value="Add Reminder" onclick=\"location.href="'.$manageurl.'"\">'

Because you are using single quotes at the beginning of the string, there is no need to add a backslash before the double quotes.

You can fix it like the example:

'<input type="button" class="btn btn-primary" value="Add Reminder" onclick="location.href=\''.$manageurl.'\'">'
MiYaMya
  • 24
  • 2