如何让Customcert插件的证书按钮在新页面打开?
修改Customcert插件按钮为新窗口打开的方法
针对你找到的view.php代码片段,只需在创建single_button实例后添加一行代码,即可实现按钮在新窗口打开:
// Create the button to download the customcert. $downloadbutton = ''; if ($canreceive) { $linkname = get_string('getcustomcert', 'customcert'); $link = new moodle_url('/mod/customcert/view.php', array('id' => $cm->id, 'downloadown' => true)); $downloadbutton = new single_button($link, $linkname, 'get', true); // 新增该行,设置按钮在新窗口打开 $downloadbutton->attributes['target'] = '_blank'; $downloadbutton->class .= ' m-b-1'; // Seems a bit hackish, ahem. $downloadbutton = $OUTPUT->render($downloadbutton); }
原理说明
Moodle的single_button类支持通过attributes数组自定义HTML属性,添加target="_blank"后,渲染生成的按钮链接会自动带上该属性,点击时将在新窗口打开目标页面。
如果上述方法因Moodle版本差异不生效,可尝试备选方案(直接修改渲染后的HTML):
// 渲染按钮后添加替换逻辑 $downloadbutton = $OUTPUT->render($downloadbutton); $downloadbutton = str_replace('<a ', '<a target="_blank" ', $downloadbutton);
内容的提问来源于stack exchange,提问作者Patricio López
相关产品推荐
相关产品推荐

