You are here

function civicrm_entity_entity_type_build in CiviCRM Entity 8.3

Implements hook_entity_type_build().

Populates supported CiviCRM Entity definitions.

File

./civicrm_entity.module, line 52
Module file for the CiviCRM Entity module.

Code

function civicrm_entity_entity_type_build(array &$entity_types) {
  $logger = \Drupal::logger('civicrm-entity');
  $supported_entities = SupportedEntities::getInfo();
  $config = \Drupal::config('civicrm_entity.settings');
  $enabled_entity_types = $config
    ->get('enabled_entity_types') ?: [];
  foreach ($supported_entities as $entity_type_id => $civicrm_entity_info) {
    $clean_entity_type_id = str_replace('_', '-', $entity_type_id);
    $civicrm_entity_name = $civicrm_entity_info['civicrm entity name'];
    if (empty($civicrm_entity_info['label property'])) {
      $logger
        ->debug(sprintf('Missing label property: %s', $entity_type_id));
      continue;
    }
    $entity_type_info = [
      'provider' => 'civicrm_entity',
      'class' => CivicrmEntity::class,
      'originalClass' => CivicrmEntity::class,
      'id' => $entity_type_id,
      'component' => $civicrm_entity_info['component'] ?? NULL,
      'civicrm_entity' => $civicrm_entity_name,
      'civicrm_entity_ui_exposed' => in_array($entity_type_id, $enabled_entity_types),
      'label' => new TranslatableMarkup('CiviCRM :name', [
        ':name' => $civicrm_entity_info['civicrm entity label'],
      ]),
      // @todo add label_singular
      // @todo add label_plural
      // @todo add label_count
      'entity_keys' => [
        'id' => 'id',
        'label' => $civicrm_entity_info['label property'],
      ],
      'base_table' => $entity_type_id,
      'admin_permission' => 'administer civicrm entity',
      'permission_granularity' => 'entity_type',
      'handlers' => [
        'storage' => CiviEntityStorage::class,
        'access' => CivicrmEntityAccessHandler::class,
        'views_data' => CivicrmEntityViewsData::class,
        'storage_schema' => CivicrmEntityStorageSchema::class,
      ],
    ];
    if (in_array($entity_type_id, $enabled_entity_types)) {
      $entity_type_info = array_merge_recursive($entity_type_info, [
        'handlers' => [
          'list_builder' => CivicrmEntityListBuilder::class,
          'view_builder' => CiviCrmEntityViewBuilder::class,
          'route_provider' => [
            'default' => CiviCrmEntityRouteProvider::class,
          ],
          'form' => [
            'default' => CivicrmEntityForm::class,
            'add' => CivicrmEntityForm::class,
            'edit' => CivicrmEntityForm::class,
            'delete' => ContentEntityDeleteForm::class,
          ],
        ],
        // Generate route paths.
        'links' => [
          'canonical' => sprintf('/%s/{%s}', $clean_entity_type_id, $entity_type_id),
          'delete-form' => sprintf('/%s/{%s}/delete', $clean_entity_type_id, $entity_type_id),
          'edit-form' => sprintf('/%s/{%s}/edit', $clean_entity_type_id, $entity_type_id),
          'add-form' => sprintf('/%s/add', $clean_entity_type_id, $entity_type_id),
          'collection' => sprintf('/admin/structure/civicrm-entity/%s', $clean_entity_type_id),
        ],
        'field_ui_base_route' => "entity.{$entity_type_id}.collection",
      ]);
    }

    // If this entity has bundle support, we define the bundle field as "bundle"
    // and will use the "bundle property" as the field to fetch field options
    // from CiviCRM with.
    //
    // @see civicrm_entity_entity_bundle_info()
    // @see \Drupal\civicrm_entity\Entity\CivicrmEntity::baseFieldDefinitions()
    if (!empty($civicrm_entity_info['bundle property'])) {
      $entity_type_info['entity_keys']['bundle'] = 'bundle';
      $entity_type_info['civicrm_bundle_property'] = $civicrm_entity_info['bundle property'];
      if (isset($entity_type_info['links'])) {

        // For entities with bundles that are exposed, add the `bundle` key to
        // the add-form route. In CiviCrmEntityRouteProvider::getAddFormRoute
        // we default the value, so that it isn't actually required in the URL.
        $entity_type_info['links']['add-form'] = sprintf('%s/{%s}', $entity_type_info['links']['add-form'], $entity_type_info['entity_keys']['bundle']);
      }
    }
    $entity_types[$entity_type_id] = new ContentEntityType($entity_type_info);
  }
}