View source
<?php
use Drupal\Core\Template\Attribute;
function simple_dialog_page_build(&$page) {
$path = drupal_get_path('module', 'simple_dialog');
$page['#attached']['css'][$path . '/css/simple_dialog.css'] = array(
'every_page' => TRUE,
);
if (\Drupal::config('simple_dialog.settings')
->get('js_all')) {
simple_dialog_attach_js($page, TRUE);
}
}
function simple_dialog_help($path, $arg) {
switch ($path) {
case 'admin/help#simple_dialog':
return t('<h2>SIMPLE DIALOG</h2>
<p>This module provides a method to load pages via AJAX into a modal dialog window that will be overlaid on the screen.</p>
<p>The module implements the jquery ui dialog plugin that is provided with Drupal 7.</p>
<h3>CONFIGURATION</h3>
<p>The configuration page is at admin/config/content/simple-dialog.</p>
<ol><li>Add simple dialog javascript files to all pages.
<p>By Default this option is selected. This option is here in case you\'re trying to limit the amount of data loaded on each page load. If you\'re not worried about that you can probably just leave this enabled. A couple things to note if you disable this setting:</p>
<ul>
<li>You will need to add the javascript files to the page manually if you want to implement the "simple-dialog" class method.</li>
<li>If you are adding simple dialog links to the page using theme(\'simple_dialog\'...), the necessary javascript is added within those functions so you should be okay.</li>
</ul>
</li>
<li>Additional Classes
<p>This option allows you to specify custom classes that will also be used to launch a modal. This can be useful if you want to use a simple class like \'popup\' to launch the modal, or perhaps if you\'re upgrading a site from d6 that used the automodal module and you just want to continue using the automodal class instead of changing all your links.
<p>A space-separated list of classes should be provided with no leading
or trailing spaces.</p>
</li>
<li>Default Dialog Settings
<p>Provide some default settings for the dialog. Defaults should be formatted the same way as you would in the "rel" tag of the link (described below under HTML Implementation)</p>
</li>
</ol>
<h3>JAVASCRIPT</h3>
<p>This module doesn\'t not bring javascript files over from the target page. If your target html needs javascript to work, You will need to make sure your javascript is either inline in the html that\'s being loaded, or in the head tag of the page you are on.</p>
<p>Additionally, there is a custom javascript event available that gets triggered after the dialog is loaded. example:</p>
<pre>
$(\'#simple-dialog-container\').bind(\'simpleDialogLoaded\', function (event) {
... do something ...
});
</pre>
<h3>HTML Implementation</h3>
<p>Add the class \'simple-dialog\' to open links in a dialog. You also need to specify \'name="{selector}"\' where the {selector} is the unique html element id of the container to load from the linked page, as well as the title attribute that will act as the title of the dialog. Any additional jquery ui dialog options can be passed through the rel tag using the format:</p>
<p>rel="{option-name1}:{value1};{option-name2}:{value2};"</p>
<p>NOTE: For the position option, if you want to pass in an array of xy values, use the syntax [{x},{y}] with no quotes or spaces.</p>
<p>example:</p>
<pre>
<a href="path/to/target/page/to/load" class="id-of-element-on-target-page-to-load" rel="width:900;resizable:false;position:[center,60]" name="content-area" title="My Dialog Title">Link</a>
</pre><p></p>
<p>The available jquery ui dialog options can be found here:</p>
<p><a href="http://jqueryui.com/demos/dialog" title="http://jqueryui.com/demos/dialog">http://jqueryui.com/demos/dialog</a></p>
<h3>Theme Implementation</h3>
<p>You can also implement a simple dialog link using the simple_dialog_link theme implementation</p>' . "\n <pre>\n \$element = array(\n '#theme' => 'simple_dialog_link',\n '#text' => 'Click Me To See The Dialog',\n '#path' => '/simple-dialog-example/target-page',\n '#selector' => 'load-this-element',\n '#title' => 'My Dialog Title',\n '#options' => array(\n 'width' => 900,\n 'resizable' => FALSE,\n 'position' => array('center', 60) // can be an array of xy values\n ),\n '#class' => array('my-link-class'),\n );\n drupal_render(\$element);\n </pre>" . '
<p>For the \'position\' option, the value can be a string or an array of xy values. Per the jquery ui dialog documentation at
<a href="http://jqueryui.com/demos/dialog/#option-position:" title="http://jqueryui.com/demos/dialog/#option-position:">http://jqueryui.com/demos/dialog/#option-position:</a></p>
<p>Specifies where the dialog should be displayed. Possible values:</p>
<ol>
<li>a single string representing position within viewport: "center", "left", "right", "top", "bottom". </li>
<li>an array containing an x,y coordinate pair in pixel offset from left, top corner of viewport (e.g. array(350,100)) </li>
<li>an array containing x,y position string values (e.g. array("right","top") for top right corner).</li>
</ol>');
}
}
function simple_dialog_permission() {
return array(
'administer simple dialog' => array(
'title' => t('Administer Simple Dialog'),
),
);
}
function simple_dialog_theme($existing, $type, $theme, $path) {
return array(
'simple_dialog_link' => array(
'variables' => array(
'text' => NULL,
'path' => NULL,
'selector' => NULL,
'title' => NULL,
'options' => array(),
'link_options' => array(),
'class' => array(),
),
'template' => 'simple-dialog-link',
),
);
}
function template_preprocess_simple_dialog_link(&$variables) {
$dialog_options = array();
if ($variables['options'] && is_array($variables['options'])) {
foreach ($variables['options'] as $option_name => $value) {
if ($option_name == 'position' && is_array($value)) {
$dialog_options[] = $option_name . ':[' . $value[0] . ',' . $value[1] . ']';
}
elseif ($value) {
$dialog_options[] = $option_name . ':' . $value;
}
else {
$dialog_options[] = $option_name . ':false';
}
}
}
$dialog_options = implode(';', $dialog_options);
array_unshift($variables['class'], 'simple-dialog');
$attributes = array(
'title' => $variables['title'],
'name' => $variables['selector'],
'rel' => $dialog_options,
'class' => $variables['class'],
);
if (!empty($variables['attributes'])) {
$attributes = array_merge($variables['attributes'], $attributes);
}
$variables['attributes'] = new Attribute($attributes);
}
function simple_dialog_attach_js(&$element, $every_page = FALSE) {
$element['#attached']['library'][] = 'system/ui.dialog';
$element['#attached']['js'][] = array(
'data' => array(
'simpleDialog' => array(
'classes' => \Drupal::config('simple_dialog.settings')
->get('classes'),
'defaults' => array(
'settings' => \Drupal::config('simple_dialog.settings')
->get('defaults.settings'),
'target_selector' => \Drupal::config('simple_dialog.settings')
->get('defaults.target_selector'),
'title' => \Drupal::config('simple_dialog.settings')
->get('defaults.title'),
),
),
),
'type' => 'setting',
);
$element['#attached']['js'][drupal_get_path('module', 'simple_dialog') . '/js/simple_dialog.js'] = array(
'every_page' => $every_page,
);
}