EntityRelationship Only working one direction

by lilott8   Last Updated December 15, 2017 19:07 PM

I have three custom entities: Company, FinancialRecord, Score.

Here are the following relationships:

  • Company has a one-to-many relationship with FinancialRecord
  • FinancialRecord has a one-to-one relationship with Score
  • Company has a one-to-many relationship with Score

When I build my view for Company I don't have access to the Score or FinancialRecord entities.

To be able to see the Company/Score relationship in the view I have to build a view starting with the Score. Is there a way to build a view off of the Company entity and still see the Score or FinancialRecored relationships?

Here is how I've built my respective content entities:

Company

/**
 * Defines the company entity class.
 *
 * There are many more properties to be used in an entity type definition. For
 * a complete overview, please refer to the '\Drupal\Core\Entity\EntityType'
 * class definition.
 *
 * @ingroup company
 *
 * @ContentEntityType(
 *   id = "company",
 *   label = @Translation("Company"),
 *   label_collection = @Translation("Company"),
 *   label_singular = @Translation("company"),
 *   label_plural = @Translation("companies"),
 *   label_count = @PluralTranslation(
 *     singular = "@count company",
 *     plural = "@count companies"
 *   ),
 *   handlers = {
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = "Drupal\financial\Entity\Controller\CompanyListBuilder",
 *     "storage_schema" = "Drupal\financial\Entity\Storage\BaseCompanyStorageSchema",
 *     "views_data" = "Drupal\views\EntityViewsData",
 *     "form" = {
 *       "add" = "Drupal\financial\Form\Entity\CompanyForm",
 *       "edit" = "Drupal\financial\Form\Entity\CompanyForm",
 *       "delete" = "Drupal\financial\Form\Entity\CompanyDeleteForm",
 *     },
 *   },
 *   base_table = "company",
 *   data_table = "company_data",
 *   fieldable = TRUE,
 *   translatable = FALSE,
 *   entity_keys = {
 *     "id" = "id",
 *     "uuid" = "uuid",
 *     "ticker" = "ticker",
 *   },
 *   links = {
 *     "canonical" = "/company/{id}",
 *     "delete-form" = "/company/{id}/delete",
 *     "edit-form" = "/company/{id}/edit",
 *     "collection" = "/company/list"
 *   },
 *   field_ui_base_route = "entity_type.company.settings",
 * )
 */
class Company extends BaseCompany implements CompanyInterface {
    // All fields are built correctly, as in, this content entity holds the data I want it to, I'm leaving unnecessary stuff out for brevity and clarity.
   /**
    * {@inheritdoc}
    *
    * Define the field properties here.
    *
    * Field name, type and size determine the table structure.
    *
    * In addition, we can define how the field and its content can be manipulated
    * in the GUI. The behaviour of the widgets used can be determined here.
    */
    public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
        $fields = parent::baseFieldDefinitions($entity_type);

        // This is an attempt to be able to see the financial record from the a view based on a Company.
        // After testing, this *doesn't* work.
        $fields['financial_id'] = BaseFieldDefinition::create('entity_reference')
        ->setLabel(t('Financial Record'))
        ->setDescription(t('Financial Record for the company.'))
        ->setSetting('target_type', 'financial')
        ->setSetting('handler_settings', ['target_bundles' => ['financial' => 'financial']])
        ->setSetting('handler', 'default')
        ->setDisplayOptions('view', array(
          'label' => 'above',
          'weight' => -3,
        ))
        ->setDisplayConfigurable('view', TRUE);

        // .... more definitions
        return $fields;
    }
}

Score

/**
 * Defines the base company entity class.
 *
 * There are many more properties to be used in an entity type definition. For
 * a complete overview, please refer to the '\Drupal\Core\Entity\EntityType'
 * class definition.
 *
 * @ingroup financial
 *
 * @ContentEntityType(
 *   id = "score",
 *   label = @Translation("Score"),
 *   label_collection = @Translation("Score"),
 *   label_singular = @Translation("score"),
 *   label_plural = @Translation("scores"),
 *   label_count = @PluralTranslation(
 *     singular = "@count score",
 *     plural = "@count scores"
 *   ),
 *   handlers = {
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = "Drupal\financial\Entity\Controller\FinancialListBuilder",
 *     "views_data" = "Drupal\views\EntityViewsData",
 *     "storage_schema" = "Drupal\financial\Entity\Storage\BaseScoreStorageSchema",
 *   },
 *   base_table = "score",
 *   data_table = "score_data",
 *   fieldable = TRUE,
 *   translatable = FALSE,
 *   entity_keys = {
 *     "id" = "id",
 *     "uuid" = "uuid",
 *     "company_id" = "company_id",
 *     "year" = "year",
 *     "fiscal_period" = "fiscal_period",
 *   },
 *   links = {
 *     "canonical" = "/score/{id}",
 *     "delete-form" = "/score/{id}/delete",
 *     "edit-form" = "/score/{id}/edit",
 *   }
 * )
 */
class Score extends BaseScore implements ScoreInterface {
    // All fields are built correctly, as in, this content entity holds the data I want it to, I'm leaving unnecessary stuff out for brevity and clarity.
    public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
        $fields = parent::baseFieldDefinitions($entity_type);
        // Company field of the scpre record.
        // Entity reference field, holds the reference to the company object.
        $fields['company_id'] = BaseFieldDefinition::create('entity_reference')
            ->setLabel(t('Company'))
            ->setDescription(t('The company who owns this record.'))
            ->setSetting('target_type', 'company')
            ->setSetting('handler_settings', ['target_bundles' => ['company' => 'company']])
            ->setSetting('handler', 'default')
            ->setDisplayOptions('view', array(
                'label' => 'above',
                'weight' => -3,
            ))
            ->setDisplayOptions('form', array(
                  'type' => 'entity_reference_autocomplete',
                  'settings' => array(
                        'match_operator' => 'CONTAINS',
                        'size' => 60,
                        'placeholder' => '',
                  ),
                  'weight' => -3,
            ))
            ->setDisplayConfigurable('form', TRUE)
            ->setDisplayConfigurable('view', TRUE);

        // ... more definitions
        return $fields;
    }
}

FinancialRecord

/**
 * Defines the financial entity class.
 *
 * There are many more properties to be used in an entity type definition. For
 * a complete overview, please refer to the '\Drupal\Core\Entity\EntityType'
 * class definition.
 *
 * @ingroup financial
 *
 * @ContentEntityType(
 *   id = "financial",
 *   label = @Translation("Financial"),
 *   label_collection = @Translation("Financial"),
 *   label_singular = @Translation("financial"),
 *   label_plural = @Translation("financials"),
 *   label_count = @PluralTranslation(
 *     singular = "@count financial",
 *     plural = "@count financials"
 *   ),
 *   handlers = {
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "views_data" = "Drupal\views\EntityViewsData",
 *   },
 *   base_table = "financial",
 *   data_table = "financial_data",
 *   fieldable = TRUE,
 *   translatable = FALSE,
 *   entity_keys = {
 *     "id" = "id",
 *     "uuid" = "uuid",
 *     "year" = "year",
 *     "fiscal_period" = "fiscal_period",
 *     "company_id" = "company_id",
 *   },
 *   links = {
 *     "canonical" = "/financial/{id}",
 *     "delete-form" = "/financial/{id}/delete",
 *     "edit-form" = "/financial/{id}/edit",
 *   }
 * )
 */
class Financial extends BaseFinancial implements FinancialInteface {

  /**
    * {@inheritdoc}
    *
    * Define the field properties here.
    *
    * Field name, type and size determine the table structure.
    *
    * In addition, we can define how the field and its content can be manipulated
    * in the GUI. The behaviour of the widgets used can be determined here.
    */
    public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
        $fields = parent::baseFieldDefinitions($entity_type);

        // Company field of the financial record.
        // Entity reference field, holds the reference to the company object.
        // The view shows the ticker field of the company.
        // The form presents an auto complete field for the company.
        $fields['company_id'] = BaseFieldDefinition::create('entity_reference')
        ->setLabel(t('Company'))
        ->setDescription(t('The company who owns this record.'))
        ->setSetting('target_type', 'company')
        ->setSetting('handler_settings', ['target_bundles' => ['company' => 'company']])
        ->setSetting('handler', 'default')
        ->setDisplayOptions('view', array(
            'label' => 'above',
            'weight' => -3,
        ))
        ->setDisplayOptions('form', array(
            'type' => 'entity_reference_autocomplete',
            'settings' => array(
                'match_operator' => 'CONTAINS',
                'size' => 60,
                'placeholder' => '',
            ),
            'weight' => -3,
        ))
        ->setDisplayConfigurable('form', TRUE)
        ->setDisplayConfigurable('view', TRUE);

        // ... more fields here

        return $fields;
}

All the relationships are present in the database (the columns exist). But when I try to create a view with a Company as the base content and try to add a relationship, I cannot see Score or FinancialRecord. I don't fully understand how to get it the way that I want -- or if it is even possible.

I've read all of The Drupal 8 Entity API documentation, but I don't see anything to get what I want.



Related Questions