Skip to content
Snippets Groups Projects
Commit 0772dc66 authored by Ivan's avatar Ivan
Browse files

Initial commit.

parents
No related branches found
No related tags found
No related merge requests found
Vue.js Drupal module
--------------------
DESCRIPTION:
The module provides a bridge between Drupal and Vue.js framework.
CONFIGURATION:
Navigate to the admin/config/development/vuejs page and set up desired versions
of the libraries. Note if you prefer installing libraries locally you need to
install them after each version change. The following drush command can be used
to quckly download required Vue.js libraries.
`drush vuejs-download <LIBRARY_NAME>`
PROJECT PAGE:
https://www.drupal.org/project/vuejs
libraries:
vue:
installation: cdnjs
minified: true
version: 2.0.1
vue_router:
installation: cdnjs
minified: true
version: 2.0.0
vue_resource:
installation: cdnjs
minified: true
version: 1.0.3
vuejs.settings:
type: config_object
label: Vue.js settings
maping:
libaries:
type: mapping
label: Libraries
mapping:
vue:
type: mapping
label: Vue settings
mapping:
installation:
type: string
label: Vue installation
minified:
type: boolean
label: minified
version:
type: string
label: Vue version
vue_router:
type: mapping
label: vue-router settings
mapping:
installation:
type: string
label: vue-router installation
minified:
type: boolean
label: minified
version:
type: string
label: vue-router version
vue_resource:
type: mapping
label: vue-resource settings
mapping:
installation:
type: string
label: vue-resource installation
minified:
type: boolean
label: minified
version:
type: string
label: vue-resourc version
<?php
namespace Drupal\vuejs\Form;
use Drupal\Core\Asset\LibraryDiscoveryInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Configure Vue.js settings for this site.
*/
class SettingsForm extends ConfigFormBase {
/**
* The library.discovery service.
*
* @var \Drupal\Core\Asset\LibraryDiscoveryInterface
*/
protected $libraryDiscovery;
/**
* Constructs a form object.
*
* @param \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery
* The discovery service.
*/
public function __construct(ConfigFactoryInterface $config_factory, LibraryDiscoveryInterface $library_discovery) {
$this->libraryDiscovery = $library_discovery;
parent::__construct($config_factory);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('library.discovery')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'vuejs_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return ['vuejs.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$registered_libraries = $this->libraryDiscovery->getLibrariesByExtension('vuejs');
$libraries = $this->config('vuejs.settings')->get('libraries');
foreach ($libraries as $library_name => $library) {
$form[$library_name] = [
'#type' => 'fieldset',
'#title' => str_replace('_', '-', $library_name),
'#tree' => TRUE,
];
$form[$library_name]['installation'] = [
'#type' => 'select',
'#title' => t('Installation'),
'#options' => [
'local' => t('Local'),
'cdnjs' => t('CDNJS'),
'jsdelivr' => t('jsDelivr'),
],
'#default_value' => $library['installation'],
];
$form[$library_name]['development'] = [
'#type' => 'checkbox',
'#title' => t('Development version'),
'#default_value' => !$library['minified'],
'#states' => [
'visible' => [
':input[name="' . $library_name . '[installation]"]' => ['value' => 'local'],
],
],
];
$form[$library_name]['version'] = [
'#type' => 'textfield',
'#title' => t('Version'),
'#size' => 9,
'#required' => TRUE,
'#default_value' => $library['version'],
];
$path = $registered_libraries[$library_name]['js'][0]['data'];
$options = [
'absolute' => TRUE,
'attributes' => ['target' => '_blank'],
];
if (strpos($path, 'libraries') === 0) {
$url = Url::fromUserInput('/' . $path, $options);
}
else {
$url = Url::fromUri($path, $options);
}
$form[$library_name]['url'] = [
'#type' => 'item',
'#title' => t('URL'),
'#markup' => (new Link($url->toString(), $url))->toString(),
];
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$libraries = $this->config('vuejs.settings')->get('libraries');
foreach ($libraries as $library_name => $library) {
$value = $form_state->getValue($library_name);
if (!preg_match('/^\d+\.\d+\.\d+$/', $value['version'])) {
$form_state->setErrorByName($library_name . '][version', $this->t('Version format is not correct.'));
}
$value['minified'] = $value['installation'] != 'local' || !$value['development'];
unset($value['url'], $value['development']);
$form_state->setValue($library_name, $value);
}
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('vuejs.settings')
->set('libraries.vue', $form_state->getValue('vue'))
->set('libraries.vue_router', $form_state->getValue('vue_router'))
->set('libraries.vue_resource', $form_state->getValue('vue_resource'))
->save();
$this->libraryDiscovery->clearCachedDefinitions();
parent::submitForm($form, $form_state);
}
}
<?php
/**
* @file
* Contains vuejs-download drush command.
*/
const VUEJS_LIBRARY_DIR = 'libraries/vuejs';
/**
* Implements hook_drush_command().
*/
function vuejs_drush_command() {
$items['vuejs-download'] = [
'description' => 'Downloads Vue.js libraries.',
'arguments' => [
'library_name' => 'Library name',
],
'required-arguments' => TRUE,
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
'aliases' => ['vue'],
'examples' => [
'drush vue-download vue-router' => 'Download vue-router library.',
],
];
return $items;
}
/**
* Callback function for vuejs-download command.
*/
function drush_vuejs_download($library_name) {
if (!is_dir('libraries')) {
return drush_set_error('VUEJS', dt('Directory libraries does not exist.'));
}
$libraries = Drupal::config('vuejs.settings')->get('libraries');
$library_machine_name = str_replace('-', '_', $library_name);
if (!isset($libraries[$library_machine_name])) {
return drush_set_error('VUEJS', dt('Library @library is not supported.', ['@library' => $library_name]));
}
$library = $libraries[$library_machine_name];
$download_dir = VUEJS_LIBRARY_DIR . '/' . $library_name . '/' . $library['version'];
if (!is_dir($download_dir)) {
drush_mkdir($download_dir);
}
foreach (['', '.min'] as $suffix) {
$download_url = sprintf(
'https://raw.githubusercontent.com/vuejs/%s/%s%s/dist/%s%s.js',
$library_name,
// Vue resource does not prefix version tags.
$library_name == 'vue-resource' ? '' : 'v',
$library['version'],
$library_name,
$suffix
);
$result = drush_shell_exec('wget --timeout=15 -O %s %s', $download_dir . '/' . $library_name . $suffix . '.js', $download_url);
if (!$result) {
return drush_set_error('VUEJS', dt('Could not download file @file.', ['@file' => $download_url]));
}
}
drush_log('Done!', 'success');
}
name: Vue.js
type: module
description: Provides a bridge between Drupal and Vue.js framework.
configure: vuejs.settings
version: 8.x-1.0-dev
core: 8.x
config.sync:
title: 'Vue.js'
description: 'Vue.js settings.'
route_name: vuejs.settings
parent: system.admin_config_development
<?php
/**
* @file
* Primary module hooks for Vue.js module.
*/
/**
* Implements hook_library_info_build().
*/
function vuejs_library_info_build() {
$libraries = \Drupal::config('vuejs.settings')->get('libraries');
foreach ($libraries as $library_name => $library) {
$libraries[$library_name] = [
'remote' => 'https://vuejs.org',
'version' => $library['version'],
'license' =>
[
'name' => 'MIT',
'url' => 'https://github.com/vuejs/vue/blob/dev/LICENSE',
'gpl-compatible' => TRUE,
],
];
switch ($library['installation']) {
case 'local':
$path = sprintf(
'/libraries/vuejs/%s/%s/%s%s.js',
str_replace('_', '-', $library_name),
$library['version'],
str_replace('_', '-', $library_name),
$library['minified'] ? '.min' : ''
);
break;
case 'cdnjs':
$path = sprintf(
'https://cdnjs.cloudflare.com/ajax/libs/%s/%s/%s.min.js',
str_replace('_', '-', $library_name),
$library['version'],
str_replace('_', '-', $library_name)
);
break;
case 'jsdelivr':
$path = sprintf(
'https://cdn.jsdelivr.net/%s/%s/%s.min.js',
str_replace('_', '.', $library_name),
$library['version'],
str_replace('_', '-', $library_name)
);
break;
}
if ($library_name != 'vue') {
$libraries[$library_name]['dependencies'] = ['vuejs/vue'];
}
$libraries[$library_name]['js'][$path] = ['minified' => $library['minified']];
}
return $libraries;
}
administer vuejs configuration:
title: Administer vuejs configuration
restrict access: true
vuejs.settings:
path: '/admin/config/development/vuejs'
defaults:
_form: 'Drupal\vuejs\Form\SettingsForm'
_title: 'Vue.js'
requirements:
_permission: 'administer vuejs configuration'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment