You are here

public function FieldDefinitionProvider::getBaseFieldDefinition in CiviCRM Entity 8.3

Gets an entity base field definition from a CiviCRM field definition.

Parameters

array $civicrm_field: The CiviCRM field definition.

Return value

\Drupal\Core\Field\BaseFieldDefinition The base field definition.

Overrides FieldDefinitionProviderInterface::getBaseFieldDefinition

File

src/Entity/FieldDefinitionProvider.php, line 15

Class

FieldDefinitionProvider

Namespace

Drupal\civicrm_entity\Entity

Code

public function getBaseFieldDefinition(array $civicrm_field) {
  if ($civicrm_field['name'] === 'contact_id' && $civicrm_field['title'] === 'Case Client') {
    $civicrm_field['FKClassName'] = 'CRM_Contact_DAO_Contact';
  }
  if ($civicrm_field['name'] == 'id') {
    $field = $this
      ->getIdentifierDefinition();
  }
  elseif (empty($civicrm_field['type'])) {
    $field = $this
      ->getDefaultDefinition();
  }
  else {
    switch ($civicrm_field['type']) {
      case \CRM_Utils_Type::T_INT:

        // Check if this is an integer representing a serial identifier and
        // is a foreign key.
        if (isset($civicrm_field['FKClassName'])) {
          $foreign_key_dao = '\\' . $civicrm_field['FKClassName'];
          $table_name = $foreign_key_dao::getTableName();

          // Verify the foreign key table is a valid entity type.
          if (array_key_exists($table_name, SupportedEntities::getInfo())) {
            $field = BaseFieldDefinition::create('entity_reference')
              ->setSetting('target_type', $foreign_key_dao::getTableName())
              ->setSetting('handler', 'default');
          }
          else {
            $field = $this
              ->getIntegerDefinition($civicrm_field);
          }
        }
        else {
          $field = $this
            ->getIntegerDefinition($civicrm_field);
        }
        break;
      case \CRM_Utils_Type::T_BOOLEAN:
        $field = $this
          ->getBooleanDefinition();
        break;
      case \CRM_Utils_Type::T_MONEY:
      case \CRM_Utils_Type::T_FLOAT:

        // @todo this needs to be handled.
        $field = BaseFieldDefinition::create('float');
        break;
      case \CRM_Utils_Type::T_STRING:
        $field = $this
          ->getStringDefinition($civicrm_field);
        break;
      case \CRM_Utils_Type::T_CCNUM:
        $field = $this
          ->getDefaultDefinition();
        break;
      case \CRM_Utils_Type::T_TEXT:
      case \CRM_Utils_Type::T_LONGTEXT:
      case \CRM_Utils_Type::T_BLOB:
        $field = $this
          ->getTextDefinition($civicrm_field);
        break;
      case \CRM_Utils_Type::T_EMAIL:
        $field = $this
          ->getEmailDefinition();
        break;
      case \CRM_Utils_Type::T_URL:
        $field = $this
          ->getUrlDefinition();
        break;
      case \CRM_Utils_Type::T_DATE:
        $field = $this
          ->getDateDefinition();
        break;
      case \CRM_Utils_Type::T_DATE + \CRM_Utils_Type::T_TIME:
      case \CRM_Utils_Type::T_TIMESTAMP:
        $field = $this
          ->getDatetimeDefinition();
        break;
      case \CRM_Utils_Type::T_ENUM:
        $field = BaseFieldDefinition::create('map');
        break;
      case \CRM_Utils_Type::T_TIME:

      // @see https://github.com/civicrm/civicrm-core/blob/master/CRM/Core/DAO.php#L279
      // When T_TIME DAO throws error?
      default:
        $field = BaseFieldDefinition::create('any');
        break;
    }
  }
  if ($civicrm_field['name'] != 'id') {
    $field
      ->setDisplayConfigurable('form', TRUE);
  }
  $field
    ->setDisplayConfigurable('view', TRUE)
    ->setLabel($civicrm_field['title'])
    ->setDescription(isset($civicrm_field['description']) ? $civicrm_field['description'] : '');
  if ($field
    ->getType() != 'boolean') {
    $field
      ->setRequired(isset($civicrm_field['api.required']) && (bool) $civicrm_field['api.required']);
  }
  if (isset($civicrm_field['api.default'])) {
    $field
      ->setDefaultValue($field['api.default']);
  }
  return $field;
}