Explorar o código

Complete config entity list, add, delete and update methods

Reynold Tan %!s(int64=3) %!d(string=hai) anos
pai
achega
f01068b9a7

+ 3 - 3
config/schema/tripal_blast.schema.yml

@@ -5,9 +5,9 @@ tripal_blast.tripalblastdatabase.*:
   type: config_entity
   label: 'BLAST Configuration Entity'
   mapping:
-    nid:
+    id:
       type: 'integer'
-      label: 'Node ID'
+      label: 'Database Entity ID'
     name:
       type: 'string'
       label: 'Name'
@@ -16,7 +16,7 @@ tripal_blast.tripalblastdatabase.*:
       label: 'Path'
     dbtype:
       type: 'string'
-      label 'Database Type'
+      label: 'Database Type'
     dbxref_id_regexp:
       type: 'string'
       label: 'DB Xref Id Regexp'

+ 21 - 4
src/Controller/TripalBlastDatabaseListBuilder.php

@@ -19,8 +19,14 @@ class TripalBlastDatabaseListBuilder extends ConfigEntityListBuilder {
    * {@inheritdoc}
    */
   public function buildHeader() {
-    $header['nid'] = $this->t('Database ID');
-    $header['name'] = $this->t('Blast Database Name');
+    $header['id'] = $this->t('Database ID');
+    $header['name'] = $this->t('Name');
+    $header['path'] = $this->t('Path');
+    $header['type'] = $this->t('Type');
+    $header['dbxref_id_regexp'] = $this->t('REGEXP Key');
+    $header['dbxref_db_id'] = $this->t('DBXref Id');
+    $header['dbxref_linkout_type'] = $this->t('Linkout Type');
+    $header['cvitjs_enabled'] = $this->t('Enable CvitJS');
 
     return $header + parent::buildHeader();
   }
@@ -29,8 +35,19 @@ class TripalBlastDatabaseListBuilder extends ConfigEntityListBuilder {
    * {@inheritdoc}
    */
   public function buildRow(EntityInterface $entity) {
-    $row['id'] = $entity->nid();
-    $row['name'] = $entity->name();
+    $row['id'] = $entity->getId();
+    $row['name'] = $entity->getName();
+    $row['path'] = $entity->getPath();
+
+    $dbtype = $entity->getDbType() == 'n' ? 'Nucleotide (n)' : 'Protein (p)';
+    $row['type'] = $dbtype;
+
+    $row['dbxref_id_regexp'] = $entity->getDbXrefRegExp();
+    $row['dbxref_db_id'] = $entity->getDbXref();
+    $row['dbxref_linkout_type'] = $entity->getDbXrefLinkout();
+
+    $enabled = $entity->getCvitjsEnabled() ? 'Enabled' : 'Disabled';
+    $row['cvitjs_enabled'] = $enabled;
 
     return $row + parent::buildRow($entity);
   }

+ 67 - 6
src/Entity/TripalBlastDatabase.php

@@ -17,17 +17,36 @@ use Drupal\tripal_blast\TripalBlastDatabaseInterface;
  *   handlers = {
  *     "list_builder" = "Drupal\tripal_blast\Controller\TripalBlastDatabaseListBuilder",
  *     "form" = {
- *       "add" = "Drupal\tripal_blast\Form\TripalBlastDatabaseForm"
+ *       "add" = "Drupal\tripal_blast\Form\TripalBlastDatabaseForm",
+ *       "edit" = "Drupal\tripal_blast\Form\TripalBlastDatabaseForm",
+ *       "delete" = "Drupal\tripal_blast\Form\TripalBlastDatabaseDeleteForm"
  *     }
  *   },
  *   admin_permission = "administer tripal",
  *   config_prefix = "tripal_blast",
  *   entity_keys = {
- *     "id" = "nid",
+ *     "id" = "id",
+ *     "name" = "name",
+ *     "path" = "path",
+ *     "dbtype" = "dbtype",
+ *     "dbxref_id_regexp" = "dbxref_id_regexp",
+ *     "dbxref_db_id" = "dbxref_db_id",
+ *     "dbxref_linkout_type" = "dbxref_linkout_type",
+ *     "cvitjs_enabled" = "cvitjs_enabled"
  *   },
  *   config_export = {
- *     "nid",
+ *     "id",
  *     "name",
+ *     "path",
+ *     "dbtype",
+ *     "dbxref_id_regexp",
+ *     "dbxref_db_id",
+ *     "dbxref_linkout_type",
+ *     "cvitjs_enabled"
+ *   },
+ *   links = {
+ *     "edit-form" = "/admin/tripal/extension/tripal_blast/configuration/tripalblastdatabase/edit/{tripalblastdatabase}",
+ *     "delete-form" = "/admin/tripal/extension/tripal_blast/configuration/tripalblastdatabase/{tripalblastdatabase}/delete"
  *   }
  * )
  */
@@ -36,7 +55,7 @@ class TripalBlastDatabase extends ConfigEntityBase implements TripalBlastDatabas
    * The primary identifier for a node.
    * @var integer
    */
-  protected $nid;
+  protected $id;
 
   /**
    * The human-readable name of the blast database.
@@ -83,8 +102,8 @@ class TripalBlastDatabase extends ConfigEntityBase implements TripalBlastDatabas
   /**
    * {@inheritdoc}
    */
-  public function getNid() {
-    return $this->nid;
+  public function getId() {
+    return $this->id;
   }
 
   /**
@@ -93,4 +112,46 @@ class TripalBlastDatabase extends ConfigEntityBase implements TripalBlastDatabas
   public function getName() {
     return $this->name;
   }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getPath() {
+    return $this->path;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getDbType() {
+    return $this->dbtype;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getDbXrefRegExp() {
+    return $this->dbxref_id_regexp;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getDbXref() {
+    return $this->dbxref_db_id;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getDbXrefLinkout() {
+    return $this->dbxref_linkout_type;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getCvitjsEnabled() {
+    return $this->cvitjs_enabled;
+  }
 }

+ 47 - 0
src/Form/TripalBlastDatabaseDeleteForm.php

@@ -0,0 +1,47 @@
+<?php
+/**
+ * @file 
+ * This is the the form to handle BLAST database deletion. 
+ */
+
+namespace Drupal\tripal_blast\Form;
+
+use Drupal\Core\Entity\EntityConfirmFormBase;
+use Drupal\Core\Url;
+use Drupal\Core\Form\FormStateInterface;
+
+/**
+ * Construct for to delete BLAST database.
+ */
+class TripalBlastDatabaseDeleteForm extends EntityConfirmFormBase {
+  /**
+   * {@inheritdoc}
+   */
+  public function getQuestion() {
+    return $this->t('Are you sure you want to delete %name?', ['%name' => $this->entity->getName()]);
+  }  
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getCancelUrl() {
+    return new Url('entity.tripal_blast.blast_database');
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getConfirmText() {
+    return $this->t('Delete');
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function submitForm(array &$form, FormStateInterface $form_state) {
+    $this->entity->delete();
+    $this->messenger()->addMessage($this->t('BLAST database %name has been deleted.', ['%name' => $this->entity->getName()]));
+
+    $form_state->setRedirectUrl($this->getCancelUrl());
+  }
+}

+ 44 - 5
src/Form/TripalBlastDatabaseForm.php

@@ -41,6 +41,7 @@ class TripalBlastDatabaseForm extends EntityForm {
    */
   public function form(array $form, FormStateInterface $form_state) {
     $form = parent::form($form, $form_state);
+    $blast_db = $this->entity;
     
     //
     // # BLAST DATABASE NAME:
@@ -49,6 +50,7 @@ class TripalBlastDatabaseForm extends EntityForm {
       '#title' => $this->t('Tripal BLAST database name'),
       '#description' => $this->t('The human-readable name of the BLAST database.'),
       '#required' => TRUE,
+      '#default_value' => $blast_db->getName()
     ];
 
     //
@@ -58,6 +60,7 @@ class TripalBlastDatabaseForm extends EntityForm {
       '#title' => $this->t('Database source path'),
       '#description' => $this->t('The full path and filename prefix of the BLAST database.'),
       '#required' => TRUE,
+      '#default_value' => $blast_db->getPath()
     ];  
 
     //
@@ -67,6 +70,7 @@ class TripalBlastDatabaseForm extends EntityForm {
       '#title' => $this->t('Database type'),
       '#options' => ['n' => 'Nucleotide', 'p' => 'Protein'],
       '#description' => $this->t('Type of the blast database (Nucleotide or Protein).'),
+      '#default_value' => $blast_db->getDbType()
     ];
 
     //
@@ -83,7 +87,8 @@ class TripalBlastDatabaseForm extends EntityForm {
         '#type' => 'textfield',
         '#title' => $this->t('Extract Regular Expression'),
         '#description' => $this->t('The Regular Expression to use to extract the id from the FASTA header of the BLAST database hit.'),
-        '#required' => TRUE,  
+        '#required' => TRUE,
+        '#default_value' => $blast_db->getDbXrefRegExp()  
       ];
 
       //
@@ -92,7 +97,8 @@ class TripalBlastDatabaseForm extends EntityForm {
         '#type' => 'textfield',
         '#title' => $this->t('BLAST database reference'),
         '#description' => $this->t('The Database records from this BLAST Database reference.'),
-        '#required' => TRUE,  
+        '#required' => TRUE,
+        '#default_value' => $blast_db->getDbXref()    
       ];
     
       //
@@ -101,7 +107,8 @@ class TripalBlastDatabaseForm extends EntityForm {
         '#type' => 'textfield',
         '#title' => $this->t('BLAST database reference linkout type'),
         '#description' => $this->t('Type of linkout to be used for this database reference.'),
-        '#required' => TRUE,  
+        '#required' => TRUE,
+        '#default_value' => $blast_db->getDbXrefLinkout()    
       ];
 
     // # SUPPORT CVITJS:
@@ -109,7 +116,7 @@ class TripalBlastDatabaseForm extends EntityForm {
       '#type' => 'checkbox',
       '#title' => $this->t('Visualize using CVITJS'),
       '#description' => $this->t('Indicate if CViTjs should be used to display hits on a whole genome.'),
-      '#default_value' => TRUE,
+      '#default_value' => $blast_db->getCvitjsEnabled()
     ]; 
           
     return $form;
@@ -119,6 +126,38 @@ class TripalBlastDatabaseForm extends EntityForm {
    * {@inheritdoc}
    */
   public function save(array $form, FormStateInterface $form_state) {
-    
+    $blast_db = $this->entity;
+
+    $blast_db->set('id', uniqid());
+    // Database Name.
+    $dbname = $form_state->getValue('fld_text_name');
+    $dbname = trim($dbname);
+    $blast_db->set('name', $dbname);
+    // Database Path.
+    $dbpath = $form_state->getValue('fld_text_path');
+    $dbpath = trim($dbpath);
+    $blast_db->set('path', $dbpath);
+    // Database Type.
+    $dbtype = $form_state->getValue('fld_text_type');
+    $dbtype = trim($dbtype);
+    $blast_db->set('dbtype', $dbtype);
+    // Database REGEXP.
+    $dbregexp = $form_state->getValue('fld_text_dbxref_id_regexp');
+    $dbregexp = trim($dbregexp);
+    $blast_db->set('dbxref_id_regexp', $dbregexp);
+    // Database XRef.
+    $dbxref = $form_state->getValue('fld_text_dbxref_db_id');
+    $dbxref = trim($dbxref);
+    $blast_db->set('dbxref_db_id', $dbxref);
+    // Database Linkout.
+    $dblinkout = $form_state->getValue('fld_text_dbxref_linkout_type');
+    $dblinkout = trim($dblinkout);
+    $blast_db->set('dbxref_linkout_type', $dblinkout);
+    // CVITJS Support.
+    $cvitjs_enabled = $form_state->getValue('fld_checkbox_cvitjs');
+    $blast_db->set('cvitjs_enabled', $cvitjs_enabled);
+
+    $blast_db->save();
+    $form_state->setRedirect('entity.tripal_blast.blast_database');
   }
 }

+ 7 - 1
src/TripalBlastDatabaseInterface.php

@@ -11,6 +11,12 @@ use Drupal\Core\Config\Entity\ConfigEntityInterface;
  * Provides an interface defining the BLAST database entity.
  */
 interface TripalBlastDatabaseInterface extends ConfigEntityInterface {  
-  public function getNid();
+  public function getId();
   public function getName();
+  public function getPath();
+  public function getDbType();
+  public function getDbXrefRegExp();
+  public function getDbXref();
+  public function getDbXrefLinkout();
+  public function getCvitjsEnabled();
 }

+ 16 - 0
tripal_blast.routing.yml

@@ -108,3 +108,19 @@ entity.tripal_blast.blast_database.add:
     _title: 'Tripal BLAST Add Query Database'
   requirements:
     _permission: 'administer tripal'
+
+entity.tripalblastdatabase.edit_form:
+  path: '/admin/tripal/extension/tripal_blast/configuration/tripalblastdatabase/edit/{tripalblastdatabase}'
+  defaults:
+    _entity_form: 'tripalblastdatabase.edit'
+    _title: 'Tripal BLAST Edit Query Database'
+  requirements:
+    _permission: 'administer tripal'
+
+entity.tripalblastdatabase.delete_form:
+  path: '/admin/tripal/extension/tripal_blast/configuration/tripalblastdatabase/{tripalblastdatabase}/delete'
+  defaults:
+    _entity_form: 'tripalblastdatabase.delete'
+    _title: 'Tripal BLAST Delete Query Database'
+  requirements:
+    _permission: 'administer tripal'