You are here

function rng_views_event_registrations in RNG - Events and Registrations 8

Same name and namespace in other branches
  1. 8.2 rng_views/rng_views.module \rng_views_event_registrations()
  2. 3.x rng_views/rng_views.module \rng_views_event_registrations()

Creates a registration list view based off `event.registrations.yml`.

YAML file contains a premade registration view with relationship to nodes. This function will overwrite node relationship with the passed entity.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The event entity type.

1 call to rng_views_event_registrations()
rng_views_form_event_type_form_alter_submit in rng_views/rng_views.module
Form submission handler.

File

rng_views/rng_views.module, line 91

Code

function rng_views_event_registrations(EntityTypeInterface $entity_type) {
  $t_args = [
    '@entity_type' => $entity_type
      ->getLabel(),
    '@entity_type_id' => $entity_type
      ->getLabel(),
  ];
  $base_path = \Drupal::moduleHandler()
    ->getModule('rng_views')
    ->getPath();
  $yaml = Yaml::decode(file_get_contents($base_path . '/extra/event.registrations.yml'));

  /** @var View $view */
  $view = \Drupal::entityTypeManager()
    ->getStorage('view')
    ->createFromStorageRecord($yaml);
  $view_id = 'rng_registrations_' . $entity_type
    ->id();
  $i = 1;
  while (View::load($view_id)) {
    $view_id = 'rng_registrations_' . $i;
    $i++;
  }
  $view
    ->set('id', $view_id)
    ->set('originalId', NULL)
    ->set('uuid', NULL)
    ->set('label', t('RNG: @entity_type: Registrations', $t_args))
    ->set('description', t('Registrations for @entity_type events.', $t_args))
    ->set('tag', implode(', ', [
    'rng',
    'event',
    'list',
    $entity_type
      ->id(),
  ]));

  // Dependencies
  $dependencies = $view
    ->get('dependencies');
  if (array_key_exists('module', $dependencies)) {

    // Remove node module from dependencies.
    $dependencies['module'] = array_diff_key($view
      ->get('dependencies')['module'], [
      'node',
    ]);
  }
  $dependencies['module'][] = $entity_type
    ->getProvider();

  // Master display.
  $defaults = $view
    ->getDisplay('default');

  // VEOA Access.
  $defaults['display_options']['access']['options']['parameter'] = $entity_type
    ->id();
  $defaults['display_options']['access']['options']['entity_type'] = $entity_type
    ->id();
  $defaults['display_options']['access']['options']['operation'] = 'manage event';

  // DER Relationship.
  $relationship = $defaults['display_options']['relationships']['node__event'];

  // $target_entity__$origin_base_field_name
  $rid = $entity_type
    ->id() . '__event';
  $defaults['display_options']['relationships'] = [
    $rid => [
      'id' => $rid,
      'field' => $entity_type
        ->id() . '__event',
      'admin_label' => t('Event: @entity_type', $t_args),
    ] + $relationship,
  ];

  // Argument
  $argument = $defaults['display_options']['arguments']['nid'];
  $aid = $entity_type
    ->getKey('id');
  $defaults['display_options']['arguments'] = [
    $aid => [
      'id' => $aid,
      'table' => $entity_type
        ->getDataTable() ?: $entity_type
        ->getBaseTable(),
      'field' => $entity_type
        ->getKey('id'),
      'relationship' => $rid,
      'entity_type' => $entity_type
        ->id(),
      'entity_field' => $entity_type
        ->getKey('id'),
    ] + $argument,
  ];

  // Routing.
  if ($canonical_path = $entity_type
    ->getLinkTemplate('canonical')) {
    $page = $view
      ->getDisplay('page_1');
    $views_canonical_path = str_replace('{' . $entity_type
      ->id() . '}', '%' . $entity_type
      ->id(), $canonical_path);
    $page['display_options']['path'] = $views_canonical_path . '/registrations';
    $page['display_options']['display_extenders']['views_advanced_routing_route']['route']['defaults']['event'] = $entity_type
      ->id();
  }
  return $view;
}