1

In my theme I want to display not only the maximum number of registrants on the full view of a node but also the number of seats still available.

What is the easiest way to get the number of registrants? In Drupal 7 I retrieved this number by passing the node id into the registration_event_count() function, does RNG provide a similar function?

1

You can access the event manager service to grab an event meta object. The event meta object contains a bunch of helpers to manipulate registrations and registrants. In your template preprocess functions add whatever data you want from the event meta object.

function mymodule_preprocess_HOOK(&$variables) {
  $event_entity = $entity; // Provide your entity

  /** @var \Drupal\rng\EventManagerInterface $event_manager */
  $event_manager = \Drupal::service('rng.event_manager');
  $event_meta = $this->eventManager->getMeta($event_entity)
  $mydata = $event_meta->whatever(); 
   // See \Drupal\rng\EventMetaInterface for methods. e.g:
   // $event_meta->getRegistrants()
   // $event_meta->getRegistrations()
   // $event_meta->countRegistrations()
   // $event_meta->getCapacity()
   // $event_meta->remainingCapacity()
  $variables['myvariable'] = $mydata;
}

For nodes, replace mymodule_preprocess_HOOK with mymodule_preprocess_node and $entity with $variables['node'].

| improve this answer | |
  • Thx! There were some errors coming from your code that I managed to fix after some experimentation, if you want you can update your answer and I can mark is as accepted. edit: putting it in answer due to lack of code support in comments – Jurriaan Roelofs Jun 26 '17 at 9:24
  • Its generalised to avoid hard-coding to nodes. RNG can support any entity, so the answer is generic. Added node example. – Daniel Jun 27 '17 at 7:17
-1

Based on answer by Daniel but adapted to work in preprocess function: (example by Daniel only works in OOP code)

/**
 * Prepares variables for node templates.
 *
 * Default template: node.html.twig.
 *
 * Calculating available spaces in event nodes with RNG registration
 *
 * @param array $variables
 *   An associative array containing:
 *   - elements: An array of elements to display in view mode.
 *   - node: The node object.
 *   - view_mode: View mode; e.g., 'full', 'teaser', etc.
 */
function mytheme_preprocess_node(&$variables) {
  $event_entity = $variables['node']; // Provide your entity

  /** @var \Drupal\rng\EventManagerInterface $event_manager */
  $event_manager = \Drupal::service('rng.event_manager');
  $event_meta = $event_manager->getMeta($event_entity);
  $mydata = $event_meta->remainingCapacity();
   // See \Drupal\rng\EventMetaInterface for methods. e.g:
   // $event_meta->getRegistrants()
   // $event_meta->getRegistrations()
   // $event_meta->countRegistrations()
   // $event_meta->getCapacity()
   // $event_meta->remainingCapacity()
  $variables['myvariable'] = $mydata;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.