You are here

public function YoastSeoWidget::settingsForm in Real-time SEO for Drupal 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/YoastSeoWidget.php \Drupal\yoast_seo\Plugin\Field\FieldWidget\YoastSeoWidget::settingsForm()

Returns a form to configure settings for the widget.

Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the widget. The field_ui module takes care of handling submitted form values.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form definition for the widget settings.

Overrides WidgetBase::settingsForm

File

src/Plugin/Field/FieldWidget/YoastSeoWidget.php, line 88

Class

YoastSeoWidget
Advanced widget for yoast_seo field.

Namespace

Drupal\yoast_seo\Plugin\Field\FieldWidget

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $element = [];

  /** @var EntityFormDisplayInterface $form_display */
  $form_display = $form_state
    ->getFormObject()
    ->getEntity();
  $entity_type = $form_display
    ->getTargetEntityTypeId();
  $bundle = $form_display
    ->getTargetBundle();
  $fields = $this->entityFieldManager
    ->getFieldDefinitions($entity_type, $bundle);
  $text_field_types = [
    'text_with_summary',
    'text_long',
    'string_long',
  ];
  $text_fields = [];
  if (empty($fields)) {
    return $elements;
  }
  foreach ($fields as $field_name => $field) {
    if (in_array($field
      ->getType(), $text_field_types)) {
      $text_fields[$field_name] = $field
        ->getLabel() . ' (' . $field_name . ')';
    }
  }
  $element['body'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Body'),
    '#required' => TRUE,
    '#description' => $this
      ->t('Select a field which is used as the body field.'),
    '#options' => $text_fields,
    '#default_value' => $this
      ->getSetting('body'),
  ];
  return $element;
}