You are here

function node_registration_entity_view in Node registration 7

Implements hook_entity_view().

File

./node_registration.module, line 117

Code

function node_registration_entity_view($entity, $entity_type, $view_mode, $langcode) {
  if ('node' == $entity_type && _node_registration_node_type_enabled($entity->type) && !empty($entity->nid)) {
    global $user;

    // Find which fields to show. Unfortunately this is necessary for new extra fields, which are
    // VISIBLE by default. Crazy Drupal...
    $bundle_settings = field_bundle_settings('node', $entity->type);
    $use_view_mode = !empty($bundle_settings['view_modes'][$view_mode]['custom_settings']) ? $view_mode : 'default';
    $show_field = function ($name) use ($bundle_settings, $use_view_mode) {
      return !empty($bundle_settings['extra_fields']['display'][$name][$use_view_mode]['visible']);
    };

    // Add link to registration page.
    $path = 'node/' . $entity->nid . '/register';
    $item = menu_get_item($path);
    $entity->content['registration_link'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'registration-link',
        ),
      ),
      '#access' => $item && !empty($item['access']),
      'link' => array(
        '#type' => 'link',
        '#title' => t('Register for this event'),
        '#href' => $path,
      ),
    );

    // Add slots left.
    $capacity = $entity->registration
      ->capacity();
    $open = $capacity - node_registration_event_count($entity);
    $entity->content['registration_slots_left'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'registration-slots-left',
        ),
      ),
      'content' => array(
        '#markup' => $capacity ? t('@num slots left.', array(
          '@num' => $open,
        )) : t('Unlimited slots left.'),
      ),
    );

    // Add reason for unregistrable.
    $access = node_registration_node_access($entity, 'register', $user, $reason);
    $entity->content['registration_unregistrable_reason'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'registration-unregistrable-reason',
        ),
      ),
      'content' => array(
        '#markup' => t('Reason for unregistrable: @reason', array(
          '@reason' => $reason ?: '?',
        )),
      ),
      '#access' => !$access && (user_access('access devel information') || isset($_GET['debug'])),
    );

    // Add link to log in to register.
    $entity->content['log_in_to_register'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'log-in-to-register',
        ),
      ),
      'content' => array(
        '#markup' => 'Bla bla bla',
      ),
      '#access' => $show_field('log_in_to_register'),
    );

    // Add registration form.
    if ($show_field('registration_form')) {

      // Create empty registration (for form).
      $registration = entity_get_controller('node_registration')
        ->create(array(
        'nid' => $entity->nid,
        'node' => $entity,
      ));

      // Check form submission access.
      $access = node_registration_access($registration, 'add');
      if ($access) {

        // Get & add form
        $form = drupal_get_form('node_registration_form', $registration);
        $entity->content['registration_form'] = array(
          '#type' => 'container',
          '#attributes' => array(
            'class' => array(
              'registration-form',
            ),
          ),
          'form' => $form,
        );
      }
    }
  }
}