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'; $response = new JsonResponse(); 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 'open': if($cid){ $this->openComposition($cid); }else{ $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); }else{ $this->error_message = t("Composition deletion needs a cid as url paramater!"); } break; } if($this->error_message){ $status = 'error'; $message = $this->error_message; } $data = array( 'action' => $action, 'status' => $status, 'message' => $this->error_message ); if($status == 'ok'){ switch ($action) { case 'create': $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(), 'cid' => $this->compo->id(), 'class' => ['composition-link'], 'title'=>$title, ), ), ); $delete_url = Url::fromRoute('edlp_studio.composition_controller_action_ajax', ['action' => 'delete', 'cid' => $this->compo->id()], ['absolute' => TRUE]); $deletelink_build = array( '#title' => t('Delete'), '#type' => 'link', '#url' => $delete_url, '#options'=>array( 'attributes' => array( 'data-drupal-link-system-path' => $delete_url->getInternalPath(), 'cid' => $this->compo->id(), 'class' => ['delete-composition-link'], 'title'=>t('Delete @title', array('@title'=>$title)), ), ), ); $data += array( 'new_name' => $title, 'new_link' => render($new_link_build), 'delete_link' => render($deletelink_build), ); break; case 'open': $this->getRendredComposition(); $data += array( 'compo' => $this->rendered_compo, ); break; case 'save': $data += array( 'name'=>$name, 'documents'=>$documents ); break; } } $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 = \Drupal::entityTypeManager()->getStorage('composition')->create($compo); $this->compo->save(); } private function getRendredComposition(){ $view_builder = \Drupal::entityTypeManager()->getViewBuilder('composition'); $compobuild = $view_builder->view($this->compo, 'studio_ui'); $this->rendered_compo = render($compobuild); } private function openComposition($cid){ $this->compo = \Drupal::entityTypeManager()->getStorage('composition')->load($cid); } private function saveComposition($cid, $name = "", $new_documents = array()){ $this->compo = \Drupal::entityTypeManager()->getStorage('composition')->load($cid); if($name != ""){ $this->compo->setName($name); } $documents = array(); if(count($new_documents)){ foreach ($new_documents as $nid) { $documents[] = ['target_id'=>$nid]; } } $this->compo->set('documents', $documents); $this->compo->save(); } private function deleteComposition($cid){ $this->compo = \Drupal::entityTypeManager()->getStorage('composition')->load($cid); $this->compo->delete(); } }