tripal / tripal_doc

Official Documentation for the Tripal Platform
https://tripaldoc.readthedocs.io/en/latest/
GNU General Public License v3.0
2 stars 3 forks source link

How to use CVterm autocomplete docs #37

Open laceysanderson opened 1 year ago

laceysanderson commented 1 year ago

We need to add documentation for how to use the Tripal core cvterm autocomplete. This was added via https://github.com/tripal/tripal/pull/1486

Here is @reynoldtan documentation from the PR:

An example form element would look this:

  $form['my_autocomplete'] = [
      '#type' => 'textfield',
      '#autocomplete_route_name' => 'tripal_chado.cvterm_autocomplete',
      '#autocomplete_route_parameters' => ['count' => 5]
   ]; 

Screen Shot 2023-04-21 at 3 53 49 PM

Since the autocomplete returns human-readable output and not the cvterm id, there is an additional method that will fetch the ID from the user input.

namespace Drupal\my_autocomplete\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\tripal_chado\Controller\ChadoCVTermAutocompleteController;

class MyAutoCompleteForm extends ConfigFormBase {
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'my_autocomplete_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
  }  

  /** 
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['my_autocomplete'] = [
      '#type' => 'textfield',
      '#autocomplete_route_name' => 'tripal_chado.cvterm_autocomplete',
      '#autocomplete_route_parameters' => ['count' => 5]
    ]; 

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $term = $form_state->getValue('my_autocomplete');

    // THIS IS THE CVTERM ID BASED ON THE USER SELECTIONS.
    $cvterm_id = ChadoCVTermAutocompleteController::getCVtermId($term);

    $this->messenger()->addWarning('Id is: ' . $cvterm_id, $repeat = FALSE);

    return parent::submitForm($form, $form_state);
  }
}

And this is what it looks like in use: video of using the example module