Tripal
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
Drupal\tripal_chado\TripalField\ChadoWidgetBase Class Reference
Inheritance diagram for Drupal\tripal_chado\TripalField\ChadoWidgetBase:
Inheritance graph
[legend]
Collaboration diagram for Drupal\tripal_chado\TripalField\ChadoWidgetBase:
Collaboration graph
[legend]

Public Member Functions

 selectSettingsForm (array $form, FormStateInterface $form_state)
 
 selectSettingsSummary ()
 
- Public Member Functions inherited from Drupal\tripal\TripalField\TripalWidgetBase
 sanitizeKey ($key)
 

Static Public Member Functions

static defaultSelectSettings ()
 
static validateAutocomplete ($element, FormStateInterface $form_state)
 

Protected Member Functions

 saveInitialValues (int $delta, string $field_name, int $linker_id, FormStateInterface &$form_state)
 
 getSelectLimit (?int $select_limit=NULL)
 
 genericSelectElement (string $pkey_column, ?int $default_id, array $options,)
 
 massageLinkingFormValues (string $fkey, array $values, FormStateInterface $form_state, string $linker_key='linker_id',)
 
 genericSelectMassageFormValues (string $pkey_id, array $values)
 
 massagePropertyFormValues (string $val, array $values, FormStateInterface $form_state, ?string $rank_term=NULL, string $linker_key='prop_id',)
 
 getMatchOperatorOptions ()
 
- Protected Member Functions inherited from Drupal\tripal\TripalField\TripalWidgetBase
 formMultipleElements (FieldItemListInterface $items, array &$form, FormStateInterface $form_state)
 

Detailed Description

Defines the Chado field widget base class.

For linking multi-cardinality fields, this class includes the following helper methods to provide support for the "remove" button added by Drupal. To enable this support, the following need to be implemented in your widget:

  1. You have the following elements in formElement():
    • a hidden value element with the key of 'record_id' that contains the base table primary key. This is used to determine if an element should be removed due to being set to empty.
    • a hidden value element with the key of 'field_name' and the value is the name of the field (see code example below).
      $field_name = $items->getFieldDefinition()->get('field_name');
      $elements['field_name'] = [
      '#type' => 'value',
      '#default_value' => $field_name,
      ];
  2. You call saveInitialValues() at the bottom of your formElement() and pass in information about the linking record.
  3. You call massageLinkingFormValues() or massagePropertyFormValues() in your massageFormValues() and indicate the requested elements. See the doc block headers for these methods for specifics.

Member Function Documentation

◆ defaultSelectSettings()

static Drupal\tripal_chado\TripalField\ChadoWidgetBase::defaultSelectSettings ( )
static

Default settings associated with the generic select element.

Returns
array The default settings.

◆ genericSelectElement()

Drupal\tripal_chado\TripalField\ChadoWidgetBase::genericSelectElement ( string  $pkey_column,
?int  $default_id,
array  $options 
)
protected

Generic select form element generator.

For a small number of values this creates a select, for many values this creates an autocomplete.

Parameters
string$pkey_columnThe name of the primary key column.
int | null$default_idThe pkey_id value of the default, if one exists.
array$options'base_table' - Chado table name 'column_name' - Column name in the base table 'type_column' - Column in base table specifying type, or single character placeholder if none exists. 'property_table' - same as base table if type is stored there 'type_id' - cvterm_id to limit by, 0 for no limiting. 'match_operator' - Either "CONTAINS" or "STARTS_WITH" 'match_limit' -Number of records that the autoselect will present 'size' - Size of the autocomplete form field 'placeholder' - Placeholder before autocomplete is filled 'select_limit' - The maximum number of records for a select. If more, then use autocomplete. Use zero if autocomplete always wanted. If NULL or empty string, then the global setting will be used.
Returns
array The appropriate form element

◆ genericSelectMassageFormValues()

Drupal\tripal_chado\TripalField\ChadoWidgetBase::genericSelectMassageFormValues ( string  $pkey_id,
array  $values 
)
protected

A helper for massageFormValues() where the generic autocomplete is used.

The genericSelectElement() will return an integer in the $values array when a select is used, but returns a string with an embedded id value in parentheses when the autocomplete is used. In this latter case, we need to return just the integer value. Note that if you somehow pass a text string without an embedded value, then no changes are made, however, this should be prevented by validation.

Parameters
string$pkey_idThe name of the value to be massaged, e.g. "analysis_id".
array$valuesThe submitted form values produced by the widget.
Returns
array The massaged values

◆ getMatchOperatorOptions()

Drupal\tripal_chado\TripalField\ChadoWidgetBase::getMatchOperatorOptions ( )
protected

Returns the options for the match operator.

Returns
array List of options.

◆ getSelectLimit()

Drupal\tripal_chado\TripalField\ChadoWidgetBase::getSelectLimit ( ?int  $select_limit = NULL)
protected

Gets the select_limit value.

This finds the value by using, in order:

  1. An explicit value passed to this function
  2. If not defined, then the value the field's settings
  3. If not defined, then the value from the global setting
  4. If not defined, then use the default value of 50
Parameters
?int$select_limit If not NULL or an empty string, use this value.
Returns
int Non-negative integer where zero is used to indicate to always use autocomplete.

◆ massageLinkingFormValues()

Drupal\tripal_chado\TripalField\ChadoWidgetBase::massageLinkingFormValues ( string  $fkey,
array  $values,
FormStateInterface  $form_state,
string  $linker_key = 'linker_id' 
)
protected

Assists the massageFormValues() function for linking fields.

Specifically, a linking field is a double-hop fields where an intermediate linking table is used. This includes properly handling deletion of the record in the linking table in chado.

Parameters
string$fkeyThe foreign key column name in the linking table. Needed because it is not guaranteed to be in $values array, e.g. for dbxref.
array$valuesThe submitted form values produced by the widget.
  • If the widget does not manage multiple values itself, the array holds the values generated by the multiple copies of the $element generated by the formElement() method, keyed by delta.
  • If the widget manages multiple values, the array holds the values of the form element generated by the formElement() method.
\Drupal\Core\Form\FormStateInterface$form_stateThe form state.
string$linker_keyThe key used for the linking table primary key. For most fields this is "linker_id" and can be omitted, but see the synonym field for an exception.
Returns
array An array of field values, keyed by delta.

◆ massagePropertyFormValues()

Drupal\tripal_chado\TripalField\ChadoWidgetBase::massagePropertyFormValues ( string  $val,
array  $values,
FormStateInterface  $form_state,
?string  $rank_term = NULL,
string  $linker_key = 'prop_id' 
)
protected

Assists the massageFormValues() function for property fields.

Specifically, a property field is a single-hop fields where the linked table contains a value. This includes properly handling deletion of the record in the linked table in chado.

Parameters
string$valThe name that the value is stored under, i.e. 'value'.
array$valuesThe submitted form values produced by the widget.
  • If the widget does not manage multiple values itself, the array holds the values generated by the multiple copies of the $element generated by the formElement() method, keyed by delta.
  • If the widget manages multiple values, the array holds the values of the form element generated by the formElement() method.
\Drupal\Core\Form\FormStateInterface$form_stateThe form state.
string$rank_termIf present, used to reset rank in values.
string$linker_keyThe key used for the linking table primary key. For property fields this is "prop_id" and can be omitted.
Returns
array An array of field values, keyed by delta.

◆ saveInitialValues()

Drupal\tripal_chado\TripalField\ChadoWidgetBase::saveInitialValues ( int  $delta,
string  $field_name,
int  $linker_id,
FormStateInterface &  $form_state 
)
protected

Saves initial form state for context to 2+ cardinality fields.

Saves some values from the initial form state when an entity is first edited for multi-cardinality linking fields. These values are needed to support the "Remove" button.

Parameters
int$deltaThe numeric index of the item.
string$field_nameThe machine name of the field used for linking the info we're saving in form_state with the values submitted by the form.
int$linker_idThe primary key value of the record in the linking table.
\Drupal\Core\Form\FormStateInterface&$form_stateThe current form state.

◆ selectSettingsForm()

Drupal\tripal_chado\TripalField\ChadoWidgetBase::selectSettingsForm ( array  $form,
FormStateInterface  $form_state 
)

Elements for the widget settings form for the generic select element.

◆ selectSettingsSummary()

Drupal\tripal_chado\TripalField\ChadoWidgetBase::selectSettingsSummary ( )

Summary of the settings for the generic select element.

◆ validateAutocomplete()

static Drupal\tripal_chado\TripalField\ChadoWidgetBase::validateAutocomplete (   $element,
FormStateInterface  $form_state 
)
static

Form element validation handler for an autocomplete field.

Parameters
array$elementThe form element being validated.
\Drupal\Core\Form\FormStateInterface$form_stateThe form state of the (entire) configuration form.

Reimplemented in Drupal\tripal_chado\Plugin\Field\FieldWidget\ChadoRelationshipWidgetDefault.


The documentation for this class was generated from the following file: