Skip to content

How to use CVterm autocomplete docs #37

@laceysanderson

Description

@laceysanderson

We need to add documentation for how to use the Tripal core cvterm autocomplete.
This was added via tripal/tripal#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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions