You are here

function redhen_contact_form_field_has_value in RedHen CRM 8

Determines if a form field has been submitted with a value.

Parameters

object $definition: The field definition as returned by \Drupal::service('entity_field.manager')->getFieldDefinitions()

array $value: The field value in the submitted form state.

Return value

boolean TRUE if the field contains a value, FALSE otherwise.

1 call to redhen_contact_form_field_has_value()
redhen_contact_user_registration_form_state in modules/redhen_contact/redhen_contact.module
Return form_state that includes the values from the visible form elements.

File

modules/redhen_contact/redhen_contact.module, line 626
Contains redhen_contact.module..

Code

function redhen_contact_form_field_has_value($definition, $value) {
  if ($definition instanceof \Drupal\Core\Field\BaseFieldDefinition) {

    // Base fields can have their values compared against the field's
    // default value. If the value matches the default, the field is empty.
    if ($value === $definition
      ->getDefaultValueLiteral()) {
      return FALSE;
    }
  }
  else {
    if ($definition instanceof \Drupal\field\Entity\FieldConfig) {

      // Some entity fields require special logic to determine if they are empty.
      // Start with the field type.
      $field_type = $definition
        ->getType();
      switch ($field_type) {

        // Test an image field.
        case 'image':
          if (empty($value[0]['fids'])) {
            return FALSE;
          }
          break;
        default:

          // Default to testing the field storage definition's first column for
          // an empty value. This works for most fields.
          // e.g. An entity_reference field would have a "target_id" column,
          // which is an array key that can be tested for emptiness in the
          // field value.
          $columns = $definition
            ->getFieldStorageDefinition()
            ->getColumns();
          reset($columns);
          $column_name = key($columns);

          // Some field types store their values in $value[0].
          // Example: string, entity_reference, and telephone type fields.
          if (isset($value[0])) {
            if (array_key_exists($column_name, $value[0]) && empty($value[0][$column_name])) {
              return FALSE;
            }
          }
          else {
            if (array_key_exists($column_name, $value) && empty($value[$column_name])) {
              return FALSE;
            }
          }
          break;
      }
    }
  }
  return TRUE;
}