You are here

public function CalendarEventController::addEvent in Fullcalendar View 8.2

Same name and namespace in other branches
  1. 8.3 src/Controller/CalendarEventController.php \Drupal\fullcalendar_view\Controller\CalendarEventController::addEvent()
  2. 6.x src/Controller/CalendarEventController.php \Drupal\fullcalendar_view\Controller\CalendarEventController::addEvent()
  3. 5.x src/Controller/CalendarEventController.php \Drupal\fullcalendar_view\Controller\CalendarEventController::addEvent()

New event handler function.

Parameters

\Symfony\Component\HttpFoundation\Request $request: Http Request object.

Return value

array A event entity form render array

1 string reference to 'CalendarEventController::addEvent'
fullcalendar_view.routing.yml in ./fullcalendar_view.routing.yml
fullcalendar_view.routing.yml

File

src/Controller/CalendarEventController.php, line 222

Class

CalendarEventController
Calendar Event Controller.

Namespace

Drupal\fullcalendar_view\Controller

Code

public function addEvent(Request $request) {
  $entity_type_id = $request
    ->get('entity', '');
  $bundle = $request
    ->get('bundle', '');
  $start_field = $request
    ->get('start_field', '');
  $end_field = $request
    ->get('end_field', '');
  $form = [];
  if (!empty($bundle) && !empty($entity_type_id)) {
    $access_control_handler = $this
      ->entityTypeManager()
      ->getAccessControlHandler($entity_type_id);

    // Check the user permission.
    if ($access_control_handler
      ->createAccess($bundle)) {
      $data = [
        'type' => $bundle,
      ];

      // Create a new event entity for this form.
      $entity = $this
        ->entityTypeManager()
        ->getStorage($entity_type_id)
        ->create($data);
      if (!empty($entity)) {

        // Add form.
        $form = $this
          ->entityFormBuilder()
          ->getForm($entity);

        // Field definitions of this entity.
        $field_def = $entity
          ->getFieldDefinitions();

        // Hide those fields we don't need for this form.
        foreach ($form as $name => &$element) {
          switch ($name) {
            case 'advanced':
            case 'body':
              $element['#access'] = FALSE;
          }

          // Hide all fields that are irrelevant to the event date.
          if (substr($name, 0, 6) === 'field_' && $name !== $start_field && $name !== $end_field && $name !== 'field_monthly_event' && $name !== 'field_weekly_event' && !$field_def[$name]
            ->isRequired()) {
            $element['#access'] = FALSE;
          }
        }

        // Hide preview button.
        if (isset($form['actions']['preview'])) {
          $form['actions']['preview']['#access'] = FALSE;
        }

        // Move the Save button to the bottom of this form.
        $form['actions']['#weight'] = 10000;
        return $form;
      }
    }
  }

  // Return access denied for users don't have the permission.
  throw new AccessDeniedHttpException();
}