You are here

public static function FivestarItem::fieldSettingsFormValidate in Fivestar 8

Validate callback: check field settings.

File

src/Plugin/Field/FieldType/FivestarItem.php, line 194

Class

FivestarItem
Plugin implementation of the 'fivestar' field type.

Namespace

Drupal\fivestar\Plugin\Field\FieldType

Code

public static function fieldSettingsFormValidate(array $form, FormStateInterface $form_state) {
  $host_entity = $form_state
    ->get('host_entity');
  $field_settings = $form_state
    ->getValue('settings');

  // Validate voting target settings.
  if ($field_settings['enable_voting_target'] == 1) {

    // Check if bridge field exist.
    if (!$host_entity
      ->hasField($field_settings['target_bridge_field'])) {
      $form_state
        ->setErrorByName('target_bridge_field', t('The host entity doesn\'t contain field: "@field_name"', [
        '@field_name' => $field_settings['target_bridge_field'],
      ]));
      return;
    }

    // Check if bridge field has correct type.
    $field_type = $host_entity
      ->get($field_settings['target_bridge_field'])
      ->getFieldDefinition()
      ->getType();
    if ($field_type != 'entity_reference') {
      $form_state
        ->setErrorByName('target_bridge_field', t('The bridge field must have "entity_reference" type. The entered field has type: "@field_type"', [
        '@field_type' => $field_type,
      ]));
      return;
    }
  }
}