Repository URL to install this package:
|
Version:
2.0.2 ▾
|
drupal/cohesion_javascript_element
/
src
/
Plugin
/
CustomElement
/
CohesionJavascriptElementForm.php
|
|---|
<?php
namespace Drupal\cohesion_javascript_element\Plugin\CustomElement;
use Drupal\cohesion_elements\CustomElementPluginBase;
/**
* Cohesion Javascript.
*
* The purpose of this module is to allow component developers to leverage Drupal's
* library system. It allows them to specify libraries defined in the *.libraries.yml
* file if the component rely's on a certain library to run any custom code.
*
* @package Drupal\cohesion\Plugin\CustomElement
*
* @CustomElement(
* id = "cohesion_javascript_element",
* label = @Translation("Custom Javascript")
* )
*/
class CohesionJavascriptElementForm extends CustomElementPluginBase
{
/**
* {@inheritdoc}
*/
public function getFields() {
// Get current fields from parent class we extend
// so we can add to them.
$fields = parent::getFields();
$fields['id'] = [
'htmlClass' => 'col-xs-6',
'type' => 'textfield',
'title' => 'ID (used for drupalSettings)',
];
$fields['scope'] = [
'htmlClass' => 'col-xs-6',
'type' => 'select',
'title' => 'Scope (Footer recommended)',
'nullOption' => 'footer',
'defaultValue' => 'footer',
'options' => [
'header' => 'Header',
'footer' => 'Footer',
]
];
$fields['javascript'] = [
'htmlClass' => 'col-xs-12',
'type' => 'textarea',
'title' => 'Add Custom Javascript',
];
$fields['add_jquery'] = [
'htmlClass' => 'col-xs-2',
'type' => 'checkbox',
'title' => 'Add jQuery',
// These fields are specific to this form field type.
'defaultValue' => true,
];
$fields['libraries'] = [
'htmlClass' => 'col-xs-10',
'type' => 'textarea',
'title' => 'Dependency Libraries (one per line)',
];
$fields['drupalSettings'] = [
'htmlClass' => 'col-xs-12',
'type' => 'textarea',
'title' => 'drupalSettings (should be valid JSON)',
];
return $fields;
}
/**
* {@inheritdoc}
*/
public function render($element_settings, $element_markup, $element_class, $element_context = []) {
$cache = &drupal_static('cohesion_javascript_element', []);
// Create form id to pass to twig template and front end settings.
$formId = !empty($element_settings['id']) ? $element_settings['id'] : $element_class;
// Create initial render array.
$renderArr = [
'#theme' => 'cohesion_javascript_element'
];
// Set some defaults for Attach Inline module.
$js = [
'data' => '',
'scope' => 'footer',
'dependencies' => []
];
// Add any attributes to <script> tag
if(isset($element_markup['attributes'])) {
foreach($element_markup['attributes'] as $item) {
$js['attributes'][$item['attribute']] = $item['value'];
}
}
if(!empty($element_settings['javascript'])) {
$js['data'] = $element_settings['javascript'] ?: '';
$js['scope'] = $element_settings['scope'] ?: 'footer';
}
// Attach the libraries to the render array.
if (!empty($element_settings['libraries'])) {
// Taken from https://stackoverflow.com/questions/17918381/how-to-explode-a-string-with-spaces-and-new-lines
$libraries = preg_split('/[\s]+/', $element_settings['libraries']);
$libraries = array_filter($libraries, function($library){
return $library != '1';
});
$js['dependencies'] = $libraries ?: [];
}
// Add jQuery as a dependency
if(!empty($element_settings['add_jquery'])) {
$js['dependencies'][] = 'core/jquery';
}
// Attach drupalSettings to the front end.
if (!empty($element_settings['drupalSettings'])) {
$drupalSettings = json_decode($element_settings['drupalSettings']);
$js['dependencies'][] = 'core/drupalSettings';
$renderArr['#attached']['drupalSettings'][$formId] = $drupalSettings;
}
$renderArr['#attached']['js'][] = $js;
// Prevent duplicate code from rendering on the page.
$encodedRenderArr = base64_encode(json_encode($renderArr));
if(in_array($encodedRenderArr, $cache)) {
return;
} else {
$cache[] = $encodedRenderArr;
drupal_static('cohesion_javascript_element', $cache);
}
// Render the element.
return $renderArr;
}
}