You are here

public function ComputedFieldItem::fieldSettingsForm in Computed Field 8

Returns a form for the field-level settings.

Invoked from \Drupal\field_ui\Form\FieldConfigEditForm to allow administrators to configure field-level settings.

Parameters

array $form: The form where the settings form is being included in.

\Drupal\Core\Form\FormStateInterface $form_state: The form state of the (entire) configuration form.

Return value

array The form definition for the field settings.

Overrides FieldItemBase::fieldSettingsForm

File

src/Plugin/Field/FieldType/ComputedFieldItem.php, line 119

Class

ComputedFieldItem
Plugin implementation of the 'computed' field type.

Namespace

Drupal\computed_field\Plugin\Field\FieldType

Code

public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
  $element = array();
  $element['computed_code'] = array(
    '#type' => 'textarea',
    '#title' => $this
      ->t('Computed Code (PHP)'),
    '#default_value' => $this
      ->getSetting('computed_code'),
    '#required' => FALSE,
    '#description' => t('<p>The variables available to your code include: <code>&$value</code>, <code>$fields</code>, <code>$entity</code>, <code>$entity_manager</code>. To set the value of the field, set <code>$value</code>. For multi-value computed fields <code>$value</code> should be an array. Here\'s a simple example which sets the computed field\'s value to the value of the sum of the number fields (<code>field_a</code> and <code>field_b</code>) in a node entity:</p><p><code>$value = $entity->field_a->value + $entity->field_b->value;</code></p><p>An alternative example:</p><p><code>$value = $fields[\'field_a\'][0][\'value\'] + $fields[\'field_b\'][0][\'value\'];</code></p>'),
  );
  $element['display_code'] = array(
    '#type' => 'textarea',
    '#title' => $this
      ->t('Display Code (PHP)'),
    '#default_value' => $this
      ->getSetting('display_code'),
    '#required' => FALSE,
    '#description' => t('This code should assign a string to the $display_output variable, which will be printed when the field is displayed. The raw computed value of the field is in <code>$value</code>. Also following variables are available: <code>$fields</code>, <code>$entity</code>, <code>$entity_manager</code>. Note: this code has no effect if you use the "Raw computed value" display formatter.'),
  );
  return $element;
}