composition sorting is now saving automaticly

This commit is contained in:
Bachir Soussi Chiadmi
2018-03-27 00:26:52 +02:00
parent 90c9c68dcd
commit 4e33e4c8af
9 changed files with 120 additions and 43 deletions
@@ -1,7 +1,7 @@
(function ($, Drupal, drupalSettings) {
var settings = drupalSettings.edlp_studio;
var composer;
var _settings = drupalSettings.edlp_studio;
var $composer;
function init(){
console.log('Studio Init');
@@ -99,7 +99,7 @@
};
function openCompo($link){
var cid = $link.attr('cid');
var ajax_path = settings.open_compo_ajax_url+'/'+cid;
var ajax_path = _settings.open_compo_ajax_url+'/'+cid;
var path = window.location.origin + Drupal.url(ajax_path);
$link.addClass('ajax-loading');
$composer.addClass('ajax-loading');
@@ -228,7 +228,7 @@
function updateStudioChutier(){
$studioChutier = $('#studio-ui .chutier_ui');
if($studioChutier.length){
var ajax_path = settings.chutier_ui_ajax;
var ajax_path = _settings.chutier_ui_ajax;
var path = window.location.origin + drupalSettings.path.baseUrl + ajax_path;
$studioChutier.addClass('loading');
$.getJSON(path, {})
@@ -335,19 +335,21 @@
function onOrderChanged(e, ui){
console.log('onOrderChanged : e', e);
// TODO: record the current order through ajax
var documents = [];
var cid = $(e.target).parents('.composition').attr('cid');
var documents = {};
$('a', e.target).each(function(i) {
// console.log(this);
documents.push($(this).attr('nid'));
documents[i] = $(this).attr('nid');
});
console.log(documents);
// recordCompoList(cid, documents);
// console.log(documents);
recordCompoList(cid, documents);
};
function recordCompoList(cid, docs){
var ajax_path = 'path';
console.log('recordCompoList '+cid, docs);
var ajax_path = _settings.save_compo_ajax_url+'/'+cid;
var path = window.location.origin + Drupal.url(ajax_path);
console.log('path', path);
$.getJSON(path, {
documents:docs
})
@@ -30,10 +30,15 @@ function edlp_studio_help($route_name, RouteMatchInterface $route_match) {
*/
function edlp_studio_page_attachments(array &$attachments) {
$attachments['#attached']['library'][] = 'edlp_studio/edlp_studio-library';
// chutier ui
$url = Url::fromRoute('edlp_studio.studio_chutier_ui_ajax', [], ['absolute' => TRUE]);
$attachments['#attached']['drupalSettings']['edlp_studio']['chutier_ui_ajax'] = $url->getInternalPath();
// open compo
$url = Url::fromRoute('edlp_studio.composition_controller_action_ajax', ['action'=>'open'], ['absolute' => TRUE]);
$attachments['#attached']['drupalSettings']['edlp_studio']['open_compo_ajax_url'] = $url->getInternalPath();
// save compo
$url = Url::fromRoute('edlp_studio.composition_controller_action_ajax', ['action'=>'save'], ['absolute' => TRUE]);
$attachments['#attached']['drupalSettings']['edlp_studio']['save_compo_ajax_url'] = $url->getInternalPath();
}
/**
@@ -43,6 +48,10 @@ function edlp_studio_theme($existing, $type, $theme, $path) {
// @see https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-module
return array(
'composition' => array(
'file' => 'includes/composition.inc',
'render element'=>'elements',
),
'edlp_chutier_ui' => array(
'file' => 'includes/edlp_chutier_ui.inc',
'variables' => array(
@@ -78,6 +87,29 @@ function edlp_studio_theme($existing, $type, $theme, $path) {
);
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function edlp_studio_theme_suggestions_composition(array $vars) {
// dpm($vars);
$suggestions = [];
/** @var \Drupal\edlp_studio\CompositionInterface $compo */
$compo = $vars['elements']['#composition'];
//
$suggestions[] = 'composition';
$suggestions[] = 'composition__' . $compo->id();
$suggestions[] = 'composition__' . $vars['elements']['#view_mode'];
$suggestions[] = 'composition__' . $compo->id() . '__' . $vars['elements']['#view_mode'];
// $suggestions[] = 'taxonomy_term__' . $term->id();
// dpm($suggestions);
return $suggestions;
}
/**
* hook_entity_extra_field_info()
*
@@ -151,19 +183,3 @@ function edlp_studio_node_view(array &$build, \Drupal\Core\Entity\EntityInterfac
}
}
}
/**
* Prepares variables for Composition templates.
*
* Default template: composition.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the user information and any
* - attributes: HTML attributes for the containing element.
*/
// function edlp_studio_preprocess_composition(array &$vars) {
// dpm($vars);
// $vars['test'] = 'test';
// }
@@ -0,0 +1,40 @@
<?php
use Drupal\Core\Render\Element;
/**
* Prepares variables for Composition templates.
*
* Default template: composition.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the user information and any
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_composition(array &$vars) {
// dpm($variables);
// Fetch Composition Entity Object.
/** @var \Drupal\edlp_studio\CompositionInterface $composition */
$composition = $vars['elements']['#composition'];
$vars['view_mode'] = $vars['elements']['#view_mode'];
// $vars['name'] = $composition->getName();
// unset($vars['elements']['name']);
// $variables['page'] = $variables['view_mode'] == 'full' && taxonomy_term_is_page($term);
$vars['url'] = $composition->url();
$vars['attributes']['cid'] = $composition->id();
$vars['attributes']['class'] = array('composition');
// Helpful $content variable for templates.
foreach (Element::children($vars['elements']) as $key) {
$vars['content'][$key] = $vars['elements'][$key];
}
// dpm($vars);
}
@@ -5,7 +5,7 @@ function template_preprocess_edlp_composition_ui(&$vars){
$view_builder = \Drupal::entityTypeManager()->getViewBuilder('composition');
$vars['composition'] = $view_builder->view($vars['lastcomposition']);
$vars['composition'] = $view_builder->view($vars['lastcomposition'], 'studio_ui');
}
@@ -52,6 +52,8 @@ class CompositionController extends ControllerBase {
$status = 'ok';
$message = 'Hello';
$response = new JsonResponse();
switch($action){
case 'create':
$name = $request->query->get('new_name');
@@ -68,6 +70,15 @@ class CompositionController extends ControllerBase {
$this->error_message = t("Composition opening needs a cid as url paramater!");
}
break;
case 'save':
if($cid){
$name = $request->query->get('new_name');
$documents = $request->query->get('documents');
$this->saveComposition($cid, $name, $documents);
}else{
$this->error_message = t("Composition saving needs a cid as query paramater!");
}
break;
case 'delete':
if($cid){
$this->deleteComposition($cid);
@@ -75,9 +86,6 @@ class CompositionController extends ControllerBase {
$this->error_message = t("Composition deletion needs a cid as url paramater!");
}
break;
// case 'rename':
//
// break;
}
if($this->error_message){
@@ -85,8 +93,6 @@ class CompositionController extends ControllerBase {
$message = $this->error_message;
}
// JSON
$response = new JsonResponse();
$data = array(
'action' => $action,
'status' => $status,
@@ -122,16 +128,19 @@ class CompositionController extends ControllerBase {
'compo' => $this->rendered_compo,
);
break;
// case 'delete':
// # code...
// break;
case 'save':
$data += array(
'name'=>$name,
'documents'=>$documents
);
break;
}
}
$response->setData($data);
return $response;
// classic html
// // classic html
// return array(
// '#markup'=>'Status : ' . $status . ' | Message : ' . $message,
// );
@@ -158,6 +167,20 @@ class CompositionController extends ControllerBase {
$this->compo = entity_load('composition', $cid);
}
private function saveComposition($cid, $name = "", $new_documents = array()){
$this->compo = entity_load('composition', $cid);
if($name != ""){
$this->compo->setName($name);
}
$documents = array();
foreach ($new_documents as $nid) {
$documents[] = ['target_id'=>$nid];
}
$this->compo->set('documents', $documents);
$this->compo->save();
}
private function deleteComposition($cid){
$this->compo = entity_load('composition', $cid);
$this->compo->delete();
@@ -15,8 +15,6 @@
* @ingroup themeable
*/
#}
<div{{ attributes.addClass('composition') }}>
{% if content %}
{{- content -}}
{% endif %}
<div{{ attributes }}>
{{ content }}
</div>
@@ -8,9 +8,7 @@
<header>
{{ composer_header }}
</header>
<section class="composition">
{{ composition }}
</section>
{{ composition }}
<section class="actions">
{{ composer_actions }}
</section>