You are here

public function SimpleMathField::buildOptionsForm in Views Simple Math Field 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/views/field/SimpleMathField.php \Drupal\views_simple_math_field\Plugin\views\field\SimpleMathField::buildOptionsForm()

Default options form that provides the label widget that all fields should have.

Overrides NumericField::buildOptionsForm

File

src/Plugin/views/field/SimpleMathField.php, line 70
Defines Drupal\views_simple_math_field\Plugin\views\field\SimpleMathField.

Class

SimpleMathField
Field handler to complete mathematical operation.

Namespace

Drupal\views_simple_math_field\Plugin\views\field

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);
  $fieldDelta = preg_replace('[\\D]', '', $this->options['id']);
  $fieldList = $this->displayHandler
    ->getFieldLabels();
  foreach ($fieldList as $key => $value) {
    if ($this->field_alias === $key && $fieldDelta < preg_replace('[\\D]', '', $key)) {
      unset($fieldList[$key]);
    }
  }
  unset($fieldList[$this->options['id']]);
  $fieldList['const'] = t('Enter a constant');
  $form['fieldset_one'] = [
    '#type' => 'fieldset',
    '#title' => t('Select the field representing the first value.'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#weight' => -10,
    '#required' => TRUE,
  ];
  $form['fieldset_one']['data_field_one'] = [
    '#type' => 'radios',
    '#title' => t('Data Field One'),
    '#options' => $fieldList,
    '#default_value' => $this->options['fieldset_one']['data_field_one'],
    '#weight' => -10,
  ];
  $form['fieldset_one']['constant_one'] = [
    '#type' => 'textfield',
    '#title' => t('Constant Value'),
    '#default_value' => $this->options['fieldset_one']['constant_one'],
    '#states' => [
      'visible' => [
        ':input[name="options[fieldset_one][data_field_one]"]' => [
          'value' => 'const',
        ],
      ],
    ],
    '#weight' => -9,
  ];
  $form['fieldset_two'] = [
    '#type' => 'fieldset',
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#title' => t('Select the field representing the second value.'),
    '#weight' => -8,
    '#required' => TRUE,
  ];
  $form['fieldset_two']['data_field_two'] = [
    '#type' => 'radios',
    '#title' => t('Data Field Two'),
    '#options' => $fieldList,
    '#default_value' => $this->options['fieldset_two']['data_field_two'],
    '#weight' => -8,
  ];
  $form['fieldset_two']['constant_two'] = [
    '#type' => 'textfield',
    '#title' => t('Constant Value'),
    '#default_value' => $this->options['fieldset_two']['constant_two'],
    '#states' => [
      'visible' => [
        ':input[name="options[fieldset_two][data_field_two]"]' => [
          'value' => 'const',
        ],
      ],
    ],
    '#weight' => -7,
  ];
  $form['operation'] = [
    '#type' => 'radios',
    '#title' => t('Operation'),
    '#options' => [
      '+' => t('Add'),
      '-' => t('Subtract'),
      '*' => t('Multiply'),
      '/' => t('Divide'),
      '%' => t('Modulo'),
      '**' => t('Power'),
    ],
    '#default_value' => $this->options['operation'],
    '#description' => t('Choose your operation.'),
    '#weight' => -6,
    '#required' => TRUE,
  ];
  $form['percentage'] = [
    '#type' => 'checkbox',
    '#title' => t('Convert to percent'),
    '#default_value' => $this->options['percentage'],
    '#description' => t('Multiplies the result by 100'),
    '#weight' => -5,
    '#states' => [
      'visible' => [
        ':input[name="options[operation]"]' => [
          'value' => '/',
        ],
      ],
    ],
  ];
  return $form;
}