ajax loading of edlp studio from custom block link ok

also refactored page layout for better display of footer
This commit is contained in:
Bachir Soussi Chiadmi
2018-03-12 16:55:13 +01:00
parent 294d660d5f
commit 119f89abca
25 changed files with 569 additions and 96 deletions
@@ -48,6 +48,7 @@
});
$link.replaceWith(data.new_link);
initAjax();
// TODO: reload Studio chutier_ui's documents list
};
function onErrorActionToChutier(jqxhr, textStatus, error, $link){
@@ -43,20 +43,22 @@ function edlp_studio_theme($existing, $type, $theme, $path) {
'edlp_chutier_ui' => array(
'file' => 'includes/edlp_chutier_ui.inc',
'variables' => array(
'documents' => array(),
'title' => t('Chutier'),
'document_nodes' => array(),
),
),
'edlp_composition_ui' => array(
'file' => 'includes/edlp_composition_ui.inc',
'variables' => array(
'title' => t('Compositon'),
'compositions' => array(),
),
),
'edlp_studio_ui' => array(
'file' => 'includes/edlp_studio_ui.inc',
'variables' => array(
'chutier' => null,
'compositions' => null,
'chutier_ui' => null,
'composition_ui' => null,
),
),
);
@@ -127,6 +129,7 @@ function edlp_studio_node_view(array &$build, \Drupal\Core\Entity\EntityInterfac
$url = Chutier::getActionsUrl($entity->id(), $user->id());
$build['chutier_actions'] = array(
'#type' => 'link',
'#title' => 'Chutier',
'#url' => $url,
'#options'=>array(
'attributes' => array(
@@ -22,3 +22,11 @@ edlp_studio.studio_ui:
_title: 'Studio User Interface'
requirements:
_permission: 'use chutier'
edlp_studio.studio_ui_ajax:
path: '/edlp/studio-ui/ajax'
defaults:
_controller: '\Drupal\edlp_studio\Controller\StudioUIController::StudioUIJson'
_title: 'Studio User Interface'
requirements:
_permission: 'use chutier'
@@ -1,12 +1,57 @@
<?php
// use Drupal\Core\Url;
use \Drupal\Core\Url;
use Drupal\Component\Utility\Unicode;
function template_preprocess_edlp_chutier_ui(&$vars){
// dpm($vars);
/*
@see https://www.drupal8.ovh/index.php/en/tutoriels/339/render-a-node-or-an-entity
*/
// $view_builder = \Drupal::entityTypeManager()->getViewBuilder($vars['entity_type']);
// $vars['content'] = $view_builder->view($vars['entity'], $vars['view_mode']);
// $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
$vars['documents'] = array (
'#theme' => 'item_list',
'#items' => [],
);
foreach($vars['document_nodes'] as $node){
// get the url
$url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], ['absolute' => TRUE]);
// get the audio file url
$field_son_values = $node->get('field_son')->getValue();
$son_fid = count($field_son_values) ? $field_son_values[0]['target_id'] : "";
$son_file = \Drupal\file\Entity\File::load($son_fid);
$son_url = null;
if($son_file){
$son_uri = $son_file->getFileUri();
$son_url = file_create_url($son_uri);
}
// get the title
$title = $node->getTitle();
if(!$title) continue;
$trunc_title = Unicode::truncate($title, 30, true, true);
// classes
$classes = array('audio-link', 'ajax-link');
// get the entries
$entrees = '';
foreach ($node->get('field_entrees')->getValue() as $key => $term) {
$entrees .= '<span class="entree" tid="'.$term['target_id'].'"></span>';
}
// build the link
$vars['documents']['#items'][] = array(
'#prefix' => '<div class="entrees">'.$entrees.'</div>',
'#title' => $trunc_title,
'#type' => 'link',
'#url' => $url,
'#options'=>array(
'attributes' => array(
'data-drupal-link-system-path' => $url->getInternalPath(),
'audio_url' => $son_url,
'nid' => $node->id(),
// 'tid' => $entrees[mt_rand(0, count($entrees) - 1)],
// 'entries_size' => count($entrees),
'class' => $classes,
'title'=>$title,
)
)
);
}
}
@@ -1,8 +1,6 @@
<?php
// use Drupal\Core\Url;
function template_preprocess_edlp_compositions_ui(&$vars){
function template_preprocess_edlp_composition_ui(&$vars){
// dpm($vars);
/*
@see https://www.drupal8.ovh/index.php/en/tutoriels/339/render-a-node-or-an-entity
@@ -1,7 +1,5 @@
<?php
// use Drupal\Core\Url;
function template_preprocess_edlp_studio_ui(&$vars){
// dpm($vars);
/*
@@ -6,6 +6,7 @@ use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\User\UserDataInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\edlp_studio\Entity\Chutier;
@@ -38,36 +39,69 @@ class StudioUIController extends ControllerBase {
);
}
/**
* Studioui.
* Studio-ui.
*
* @return string
* Return Hello string.
* @return array
* Return renderable array.
*/
public function StudioUI() {
return $this->buildUI();
}
/**
* Studio-ui.
*
* @return array
* Return renderable array.
*/
public function StudioUIJson() {
$renderable = $this->buildUI();
$rendered = render($renderable);
// JSON
$response = new JsonResponse();
$response->setData([
'rendered'=> $rendered,
]);
return $response;
}
public function buildUI(){
// get current user
// done by DependencyInjection of AccountInterface
dpm($this->user->id());
// dpm($this->user->id());
// get his docs in chutier
$docs = Chutier::getUserChutiersContents($this->user->id());
dpm($docs);
$documents_nids = Chutier::getUserChutiersContents($this->user->id());
// dpm($documents_nids);
// TODO: add chutier theme, preprocess, twig template to studio module
// TODO: build content renderable array
$documents = entity_load_multiple('node', $documents_nids);
// build content renderable array
$chutier_ui = array(
"#theme"=>'edlp_chutier_ui',
'#title' => t('Chutier'),
"#document_nodes" => $documents,
);
// TODO: get compositions
// TODO: add compositions theme, preprocess, twig template to studio module
// TODO: build content renderable array
$composition_ui = array(
"#theme"=>'edlp_compostion_ui',
"#title" => t('Composition'),
"#compositions" => array('#markup'=>'TODO : compositions UI'),
);
// TODO: add studio theme, preprocess, twig template to studio module
// TODO: build global studio renderable array
return [
'#type' => 'markup',
'#markup' => $this->t('Implement method: StudioUI')
'#theme' => 'edlp_studio_ui',
'#chutier_ui' => $chutier_ui,
'#composition_ui' => $composition_ui,
];
}
}
@@ -0,0 +1,71 @@
<?php
namespace Drupal\edlp_studio\Plugin\Block;
use Drupal\Core\Session\AccountProxy;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Url;
/**
* Provides a 'StudioLinkBlock' block.
*
* @Block(
* id = "edlp_studio_link_block",
* admin_label = @Translation("Studio link block"),
* )
*/
class StudioLinkBlock extends BlockBase implements ContainerFactoryPluginInterface{
protected $user;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
// Instantiates this form class.
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('current_user')
);
}
/**
* @param array $configuration
* @param string $plugin_id
* @param mixed $plugin_definition
* @param \Drupal\Core\Session\AccountProxyInterface $account
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, AccountProxyInterface $account) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->user = $account;
}
/**
* {@inheritdoc}
*/
public function build() {
$build = [];
if($this->user->id()){
$url = Url::fromRoute('edlp_studio.studio_ui', [], ['absolute' => TRUE]);
$build['edlp_studio_link_block'] = array(
'#title' => "Studio",
'#type' => 'link',
'#url' => $url,
'#options'=>array(
'attributes' => array(
'data-drupal-link-system-path' => $url->getInternalPath(),
'class' => array('ajax-link'),
)
)
);
}
return $build;
}
}
@@ -0,0 +1,6 @@
<div class="chutier_ui">
<h2>{{ title }}</h2>
<section class="documents">
{{ documents }}
</section>
</div>
@@ -0,0 +1,6 @@
<div class="composition_ui">
<h2>{{ title }}</h2>
<section class="composition">
{{ compostions }}
</section>
</div>
@@ -0,0 +1,4 @@
<div id="studio-ui">
{{ chutier_ui }}
{{ composition_ui }}
</div>