diff --git a/src/Entity/CivicrmBaseFieldDefinition.php b/src/Entity/CivicrmBaseFieldDefinition.php new file mode 100644 index 00000000..6c86ba24 --- /dev/null +++ b/src/Entity/CivicrmBaseFieldDefinition.php @@ -0,0 +1,26 @@ +getSetting('civicrm_entity_field_metadata'); + if (is_array($metadata)) { + return [ + $metadata['column_name'] => $columns['value'] + ]; + } + return $columns; + } + +} diff --git a/src/Entity/FieldDefinitionProvider.php b/src/Entity/FieldDefinitionProvider.php index 0c8f8bf4..5d0145ff 100644 --- a/src/Entity/FieldDefinitionProvider.php +++ b/src/Entity/FieldDefinitionProvider.php @@ -3,7 +3,6 @@ namespace Drupal\civicrm_entity\Entity; use Drupal\civicrm_entity\SupportedEntities; -use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem; @@ -36,7 +35,7 @@ public function getBaseFieldDefinition(array $civicrm_field) { $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') + $field = CivicrmBaseFieldDefinition::create('entity_reference') ->setSetting('target_type', $foreign_key_dao::getTableName()) ->setSetting('handler', 'default'); if (!empty($civicrm_field['pseudoconstant'])) { @@ -48,7 +47,7 @@ public function getBaseFieldDefinition(array $civicrm_field) { } } elseif (isset($civicrm_field['data_type']) && $civicrm_field['data_type'] === 'ContactReference') { - $field = BaseFieldDefinition::create('entity_reference') + $field = CivicrmBaseFieldDefinition::create('entity_reference') ->setSetting('target_type', 'civicrm_contact') ->setSetting('handler', 'default'); @@ -70,7 +69,7 @@ public function getBaseFieldDefinition(array $civicrm_field) { case \CRM_Utils_Type::T_MONEY: case \CRM_Utils_Type::T_FLOAT: // @todo this needs to be handled. - $field = BaseFieldDefinition::create('float'); + $field = CivicrmBaseFieldDefinition::create('float'); break; case \CRM_Utils_Type::T_STRING: @@ -105,14 +104,14 @@ public function getBaseFieldDefinition(array $civicrm_field) { break; case \CRM_Utils_Type::T_ENUM: - $field = BaseFieldDefinition::create('map'); + $field = CivicrmBaseFieldDefinition::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'); + $field = CivicrmBaseFieldDefinition::create('any'); break; } } @@ -139,11 +138,11 @@ public function getBaseFieldDefinition(array $civicrm_field) { /** * Gets the identifier field definition. * - * @return \Drupal\Core\Field\BaseFieldDefinition + * @return \Drupal\civicrm_entity\Entity\CivicrmBaseFieldDefinition * The base field definition. */ protected function getIdentifierDefinition() { - return BaseFieldDefinition::create('integer') + return CivicrmBaseFieldDefinition::create('integer') ->setReadOnly(TRUE) ->setSetting('unsigned', TRUE); } @@ -158,12 +157,12 @@ protected function getIdentifierDefinition() { * @param array $civicrm_field * The CiviCRM field definition. * - * @return \Drupal\Core\Field\BaseFieldDefinition + * @return \Drupal\civicrm_entity\Entity\CivicrmBaseFieldDefinition * The base field definition. */ protected function getIntegerDefinition(array $civicrm_field) { if (!empty($civicrm_field['pseudoconstant']) && $civicrm_field['name'] != 'card_type_id') { - $field = BaseFieldDefinition::create('list_integer') + $field = CivicrmBaseFieldDefinition::create('list_integer') ->setSetting('allowed_values_function', 'civicrm_entity_pseudoconstant_options') ->setDisplayOptions('view', [ 'label' => 'hidden', @@ -177,7 +176,7 @@ protected function getIntegerDefinition(array $civicrm_field) { } // Otherwise it is just a regular integer field. else { - $field = BaseFieldDefinition::create('integer') + $field = CivicrmBaseFieldDefinition::create('integer') ->setDisplayOptions('view', [ 'label' => 'hidden', 'type' => 'number_integer', @@ -194,11 +193,11 @@ protected function getIntegerDefinition(array $civicrm_field) { /** * Gets a boolean field definition. * - * @return \Drupal\Core\Field\BaseFieldDefinition + * @return \Drupal\civicrm_entity\Entity\CivicrmBaseFieldDefinition * The base field definition. */ protected function getBooleanDefinition() { - return BaseFieldDefinition::create('boolean') + return CivicrmBaseFieldDefinition::create('boolean') ->setDisplayOptions('form', [ 'type' => 'boolean_checkbox', 'settings' => [ @@ -218,12 +217,12 @@ protected function getBooleanDefinition() { * @param array $civicrm_field * The CiviCRM field definition. * - * @return \Drupal\Core\Field\BaseFieldDefinition + * @return \Drupal\civicrm_entity\Entity\CivicrmBaseFieldDefinition * The base field definition. */ protected function getStringDefinition(array $civicrm_field) { if (!empty($civicrm_field['pseudoconstant'])) { - $field = BaseFieldDefinition::create('list_string') + $field = CivicrmBaseFieldDefinition::create('list_string') ->setSetting('allowed_values_function', 'civicrm_entity_pseudoconstant_options') ->setDisplayOptions('view', [ 'type' => 'list_default', @@ -236,7 +235,7 @@ protected function getStringDefinition(array $civicrm_field) { } // Otherwise it is just a regular integer field. else { - $field = BaseFieldDefinition::create('string') + $field = CivicrmBaseFieldDefinition::create('string') ->setDisplayOptions('view', [ 'label' => 'hidden', 'type' => 'text_default', @@ -269,7 +268,7 @@ protected function getStringDefinition(array $civicrm_field) { * @param array $civicrm_field * The CiviCRM field definition. * - * @return \Drupal\Core\Field\BaseFieldDefinition + * @return \Drupal\civicrm_entity\Entity\CivicrmBaseFieldDefinition * The base field definition. */ protected function getTextDefinition(array $civicrm_field) { @@ -288,7 +287,7 @@ protected function getTextDefinition(array $civicrm_field) { $field_type = 'text_long'; } - $field = BaseFieldDefinition::create($field_type) + $field = CivicrmBaseFieldDefinition::create($field_type) ->setDisplayOptions('view', [ 'type' => $field_type == 'string_long' ? 'basic_string' : 'text_default', 'weight' => 0, @@ -305,11 +304,11 @@ protected function getTextDefinition(array $civicrm_field) { /** * Gets an email field definition. * - * @return \Drupal\Core\Field\BaseFieldDefinition + * @return \Drupal\civicrm_entity\Entity\CivicrmBaseFieldDefinition * The base field definition. */ protected function getEmailDefinition() { - return BaseFieldDefinition::create('email') + return CivicrmBaseFieldDefinition::create('email') ->setDisplayOptions('view', [ 'label' => 'above', 'type' => 'string', @@ -324,11 +323,11 @@ protected function getEmailDefinition() { /** * Gets a URL field definition. * - * @return \Drupal\Core\Field\BaseFieldDefinition + * @return \Drupal\civicrm_entity\Entity\CivicrmBaseFieldDefinition * The base field definition. */ protected function getUrlDefinition() { - return BaseFieldDefinition::create('uri') + return CivicrmBaseFieldDefinition::create('uri') ->setDisplayOptions('form', [ 'type' => 'uri', 'weight' => 0, @@ -342,11 +341,11 @@ protected function getUrlDefinition() { /** * Gets a date field definition. * - * @return \Drupal\Core\Field\BaseFieldDefinition + * @return \Drupal\civicrm_entity\Entity\CivicrmBaseFieldDefinition * The base field definition. */ protected function getDateDefinition() { - return BaseFieldDefinition::create('datetime') + return CivicrmBaseFieldDefinition::create('datetime') ->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE) ->setDisplayOptions('form', [ 'type' => 'datetime_default', @@ -361,11 +360,11 @@ protected function getDateDefinition() { /** * Gets a datetime field definition. * - * @return \Drupal\Core\Field\BaseFieldDefinition + * @return \Drupal\civicrm_entity\Entity\CivicrmBaseFieldDefinition * The base field definition. */ protected function getDatetimeDefinition() { - $field = BaseFieldDefinition::create('datetime') + $field = CivicrmBaseFieldDefinition::create('datetime') ->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME) ->setDisplayOptions('form', [ 'type' => 'datetime_default', @@ -383,11 +382,11 @@ protected function getDatetimeDefinition() { * * This is used for CiviCRM field types which are not mappable. * - * @return \Drupal\Core\Field\BaseFieldDefinition + * @return \Drupal\civicrm_entity\Entity\CivicrmBaseFieldDefinition * The base field definition. */ protected function getDefaultDefinition() { - return BaseFieldDefinition::create('string') + return CivicrmBaseFieldDefinition::create('string') ->setDisplayOptions('view', [ 'label' => 'hidden', 'type' => 'text_default', diff --git a/src/Plugin/views/field/CustomEntityField.php b/src/Plugin/views/field/CustomEntityField.php index 87d262a8..a28a780f 100644 --- a/src/Plugin/views/field/CustomEntityField.php +++ b/src/Plugin/views/field/CustomEntityField.php @@ -335,4 +335,20 @@ protected function getDelta($id) { return 0; } + /** + * {@inheritdoc} + */ + protected function getFieldDefinition() { + if (!$this->fieldDefinition) { + // The parent method always replaces the storage definition class with + // an instance of `BaseFieldDefinition`, causing us to lose the custom + // column names. + $field_storage = $this->getFieldStorageDefinition(); + if ($field_storage instanceof FieldDefinitionInterface) { + $this->fieldDefinition = $field_storage; + } + } + return parent::getFieldDefinition(); + } + }