You are here

public function FieldCollectionHandlerRelationship::buildOptionsForm in Field collection 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/views/relationship/FieldCollectionHandlerRelationship.php \Drupal\field_collection\Plugin\views\relationship\FieldCollectionHandlerRelationship::buildOptionsForm()

Add a delta selector for multiple fields.

Overrides RelationshipPluginBase::buildOptionsForm

File

src/Plugin/views/relationship/FieldCollectionHandlerRelationship.php, line 32

Class

FieldCollectionHandlerRelationship
Relationship handler to return the taxonomy terms of nodes.

Namespace

Drupal\field_collection\Plugin\views\relationship

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);
  $field = FieldStorageConfig::loadByName($this->definition['target entity type'], $this->definition['field name']);
  $cardinality = $field
    ->getCardinality();

  // Only add the delta selector if the field is multiple.
  if ($field
    ->isMultiple()) {
    $max_delta = $cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED ? 10 : $cardinality;
    $options = [
      '-1' => t('All'),
    ];
    for ($i = 0; $i < $max_delta; $i++) {
      $options[$i] = $i + 1;
    }
    $form['delta'] = [
      '#type' => 'select',
      '#options' => $options,
      '#default_value' => $this->options['delta'],
      '#title' => t('Delta'),
      '#description' => t('The delta allows you to select which item in a multiple value field to key the relationship off of. Select "1" to use the first item, "2" for the second item, and so on. If you select "All", each item in the field will create a new row, which may appear to cause duplicates.'),
    ];
  }
}