now diplaying composition list + new compo link which is working ajaxly

This commit is contained in:
Bachir Soussi Chiadmi
2018-03-13 00:05:49 +01:00
parent 76dd9ddbc9
commit 4b8d6d0a65
13 changed files with 460 additions and 26 deletions
@@ -0,0 +1,127 @@
<?php
namespace Drupal\edlp_studio\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\User\UserDataInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\Url;
use Drupal\edlp_studio\Entity\Compositon;
/**
* Class CompositionController.
*/
class CompositionController extends ControllerBase {
protected $user;
protected $userdata;
/**
* Class constructor.
*/
public function __construct(AccountInterface $account, UserDataInterface $userdata) {
$this->user = $account;
$this->userdata = $userdata;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
// Instantiates this form class.
return new static(
// Load the service required to construct this class.
$container->get('current_user'),
$container->get('user.data')
);
}
/**
* AdddContent.
*
* @return json
* Return status.
*/
public function CompositionActionJson($action, $cid, Request $request) {
$this->error_message = null;
$status = 'ok';
$message = 'Hello';
switch($action){
case 'create':
$name = $request->query->get('new_name');
if($name){
$this->createComposition($name);
}else{
$this->error_message = t("Composition creation needs a name as query paramater!");
}
break;
// case 'delete':
//
// break;
// case 'rename':
//
// break;
}
if($this->error_message){
$status = 'error';
}
if($status == 'error'){
$message = $this->error_message;
}else{
$url = Url::fromRoute('entity.composition.canonical', ['composition' => $this->compo->id()], ['absolute' => TRUE]);
$title = $this->compo->getName();
$new_link_build = array(
'#title' => $title,
'#type' => 'link',
'#url' => $url,
'#options'=>array(
'attributes' => array(
'data-drupal-link-system-path' => $url->getInternalPath(),
'nid' => $this->compo->id(),
'class' => ['composition-link'],
'title'=>$title,
),
),
);
$new_link = render($new_link_build);
}
// JSON
$response = new JsonResponse();
$data = array(
'action' => $action,
'status' => $status,
'message' => $this->error_message,
'new_name' => $request->query->get('new_name'),
'new_link' => $new_link,
);
$response->setData($data);
return $response;
// classic html
// return array(
// '#markup'=>'Status : ' . $status . ' | Message : ' . $message,
// );
}
private function createComposition($name){
$compo = array(
'id' => NULL,
'uid' => $this->user->id(),
'status' => TRUE,
'name' => $name,
);
$this->compo = entity_create('composition', $compo);
$this->compo->save();
}
}
@@ -6,6 +6,7 @@ use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\User\UserDataInterface;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\edlp_studio\Entity\Chutier;
@@ -46,7 +47,7 @@ class StudioUIController extends ControllerBase {
* Return renderable array.
*/
public function StudioUI() {
return $this->buildUI();
return $this->buildStudioUI();
}
/**
@@ -107,15 +108,36 @@ class StudioUIController extends ControllerBase {
}
private function buildCompostionUI(){
// TODO: get compositions
// TODO: add compositions theme, preprocess, twig template to studio module
// TODO: build content renderable array
// build content renderable array
$composition_ui = array(
"#theme"=>'edlp_compostion_ui',
"#theme"=>'edlp_composition_ui',
"#title" => t('Composition'),
"#compositions" => array('#markup'=>'TODO : compositions UI'),
"#compositions" => $this->buildCompostionsList(),
"#composer" => $this->buildComposer(),
);
return $composition_ui;
}
private function buildCompostionsList(){
// get compositions
$query = \Drupal::entityQuery('composition')
->condition('user_id', $this->user->id());
$compos_nids = $query->execute();
$compos = entity_load_multiple('composition', $compos_nids);
$createurl = Url::fromRoute('edlp_studio.composition_controller_action_ajax', ['action' => 'create'], ['absolute' => TRUE]);
return array(
'#theme' => 'edlp_compositions_list',
'#composition_entities' => $compos,
'#new_composition_url' => $createurl,
);
}
private function buildComposer(){
return array('#markup'=>'TODO : composer UI');
}
}
@@ -44,10 +44,10 @@ use Drupal\user\UserInterface;
* "status" = "status",
* },
* links = {
* "canonical" = "/admin/structure/studio/composition/{composition}",
* "add-form" = "/admin/structure/studio/composition/add",
* "edit-form" = "/admin/structure/studio/composition/{composition}/edit",
* "delete-form" = "/admin/structure/studio/composition/{composition}/delete",
* "canonical" = "/composition/{composition}",
* "add-form" = "/composition/add",
* "edit-form" = "/composition/{composition}/edit",
* "delete-form" = "/composition/{composition}/delete",
* "collection" = "/admin/structure/studio/composition",
* },
* field_ui_base_route = "composition.settings"