You are here

public static function LearningPathManagerController::ajaxFormEntityCallback in Opigno Learning path 8

Same name and namespace in other branches
  1. 3.x src/Controller/LearningPathManagerController.php \Drupal\opigno_learning_path\Controller\LearningPathManagerController::ajaxFormEntityCallback()

Form ajax callback.

File

src/Controller/LearningPathManagerController.php, line 84

Class

LearningPathManagerController
Controller for all the actions of the Learning Path manager app.

Namespace

Drupal\opigno_learning_path\Controller

Code

public static function ajaxFormEntityCallback(&$form, FormStateInterface $form_state) {
  $response = new AjaxResponse();

  // If errors, returns the form with errors and messages.
  if ($form_state
    ->hasAnyErrors()) {

    // Add class for displaying form errors in iframe.
    $form['#attributes']['class'][] = 'lp-content-item-errors';
    return $form;
  }
  $entity = $form_state
    ->getBuildInfo()['callback_object']
    ->getEntity();

  // Load image.
  $media = $entity
    ->get('field_course_media_image')->entity;
  $file = isset($media) ? File::load($media
    ->get('field_media_image')->getValue[0]['target_id']) : NULL;
  $item = [];
  $item['cid'] = $entity
    ->id();
  $item['contentType'] = \Drupal::routeMatch()
    ->getParameter('type');
  $item['entityId'] = $entity
    ->id();
  $item['entityBundle'] = \Drupal::routeMatch()
    ->getParameter('type');
  $item['title'] = $entity
    ->get('label')
    ->getString();
  $item['imageUrl'] = $file ? file_create_url($file
    ->getFileUri()) : '';
  $response
    ->addCommand(new SettingsCommand([
    'formValues' => $item,
    'messages' => \Drupal::messenger()
      ->all(),
  ], TRUE));
  \Drupal::messenger()
    ->deleteAll();
  return $response;
}