View source
<?php
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\views\Entity\View;
use Drupal\Core\Form\FormStateInterface;
function rng_views_form_event_type_form_alter(&$form, FormStateInterface &$form_state) {
$form_object = $form_state
->getFormObject();
if ($form_object
->getOperation() != 'edit') {
return;
}
$form['actions']['submit']['#submit'][] = 'rng_views_form_event_type_form_alter_submit';
$event_type = $form_object
->getEntity();
$entity_type = NULL;
if (!empty($event_type
->getEventEntityTypeId())) {
$entity_type = \Drupal::entityTypeManager()
->getDefinition($event_type
->getEventEntityTypeId());
}
$form['views'] = array(
'#type' => 'fieldset',
'#title' => t('Views'),
'#tree' => TRUE,
'#weight' => 20,
);
$title = t('Create registration list');
if ($entity_type && ($view = View::load('rng_registrations_' . $entity_type
->id()))) {
if ($view
->hasLinkTemplate('edit-form')) {
$title = t('Create registration list (<a href="@view_link">view already exists</a>)', [
'@view_link' => $view
->urlInfo()
->toString(),
]);
}
else {
$title = t('Create registration list (view already exists)');
}
}
$form['views']['registration_list'] = [
'#type' => 'checkbox',
'#title' => $title,
'#description' => t('Lists all registrations for an event. View is visible as a tab on event entity pages.'),
];
}
function rng_views_form_event_type_form_alter_submit($form, FormStateInterface $form_state) {
$form_object = $form_state
->getFormObject();
$event_type = $form_object
->getEntity();
$entity_type_id = $event_type
->getEventEntityTypeId();
if ($entity_type = \Drupal::entityManager()
->getDefinition($event_type
->getEventEntityTypeId())) {
$views = $form_state
->getValue('views');
if ($views['registration_list']) {
$view = rng_views_event_registrations($entity_type);
$view
->save();
drupal_set_message(t('<a href=":url">%label</a> view created.', [
'%label' => $view
->label(),
':url' => $view
->toUrl()
->toString(),
]));
}
}
}
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'));
$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 = $view
->get('dependencies');
if (array_key_exists('module', $dependencies)) {
$dependencies['module'] = array_diff_key($view
->get('dependencies')['module'], [
'node',
]);
}
$dependencies['module'][] = $entity_type
->getProvider();
$defaults = $view
->getDisplay('default');
$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';
$relationship = $defaults['display_options']['relationships']['node__event'];
$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 = $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,
];
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;
}