Skip to content

Instantly share code, notes, and snippets.

@edutrul
Created November 10, 2016 14:35
Show Gist options
  • Save edutrul/c9df9becb353bd421fe8a1aa7bb49475 to your computer and use it in GitHub Desktop.
Save edutrul/c9df9becb353bd421fe8a1aa7bb49475 to your computer and use it in GitHub Desktop.
¿Cómo crear ventanas modales en drupal 8? (Actualizado)
<?php
// fyi: path src/Controller/DefaultConotroller.php
/**
* @file
* Contains Drupal\mymodule\Controller\DefaultController.
*/
namespace Drupal\mymodule\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Url;
use Drupal\Core\Link;
/**
* Class DefaultController.
*
* @package Drupal\mymodule\Controller
*/
class DefaultController extends ControllerBase {
/**
* Index.
*
* @return string
* Return Hello string.
*/
public function index() {
$attributes = [
'attributes' => [
'class' => ['use-ajax'],
'data-accepts' => 'application/vnd.drupal-dialog',
],
];
// Let's create the link.
$url = Url::fromRoute('mymodule.modal', [], $attributes);
$internal_link = Link::fromTextAndUrl($this->t('Open Modal'), $url)->toString();
return [
'#type' => 'markup',
'#markup' => $internal_link,
'#attached' => ['library' => ['core/drupal.dialog.ajax']]
];
}
/**
* Displays modal.
*/
public function modal() {
$response = new AjaxResponse();
$title = $this->t('Title of Modal');
$content['#markup'] = 'Content of Modal';
$response->addCommand(new OpenModalDialogCommand($title, $content));
return $response;
}
}
name: mymodule
type: module
description: My Awesome Module
core: 8.x
package: Custom
mymodule.default_controller_index:
path: '/mymodule/index'
defaults:
_controller: '\Drupal\mymodule\Controller\DefaultController::index'
_title: 'mymodule Title'
requirements:
_permission: 'access content'
mymodule.modal:
path: '/mymodule/modal'
defaults:
_controller: '\Drupal\mymodule\Controller\DefaultController::modal'
_title: 'mymodule Title'
requirements:
_permission: 'access content'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment