You are here

public function AjaxController::json in Paragraphs table 8

Return output JSON object value.

2 calls to AjaxController::json()
AjaxController::ajax in src/Controller/AjaxController.php
Return html render field paragraphs.
AjaxController::jsondata in src/Controller/AjaxController.php
Return output JSON data value.
1 string reference to 'AjaxController::json'
paragraphs_table.routing.yml in ./paragraphs_table.routing.yml
paragraphs_table.routing.yml

File

src/Controller/AjaxController.php, line 33

Class

AjaxController
Returns responses for paragraphs item routes.

Namespace

Drupal\paragraphs_table\Controller

Code

public function json($field_name, $host_type, $host_id, $typeData = FALSE) {
  $message = [];
  $entity = $this
    ->entityTypeManager()
    ->getStorage($host_type)
    ->load($host_id);
  if (empty($entity)) {
    $message[] = $this
      ->t("%type %id doesn't exist", [
      "%type" => $host_type,
      "%id" => $host_id,
    ]);
  }
  $paragraph_field = $entity
    ->hasField($field_name) ? $entity->{$field_name} : FALSE;
  if (empty($paragraph_field)) {
    $message[] = $this
      ->t("%field that does not exist on entity type %type", [
      "%field" => $field_name,
      "%type" => $host_type,
    ]);
  }
  if ($paragraph_field && ($paragraph_field_items = $paragraph_field
    ->referencedEntities())) {
    $setting = $this
      ->getFieldSetting($entity, $field_name);
    $hasPermission = ParagraphsTableFormatter::checkPermissionOperation($entity, $field_name);
    if ($hasPermission) {
      $setting['show_operation'] = TRUE;
    }
    $components = $this
      ->getComponents($paragraph_field, $setting['view_mode']);
    $typeLabel = \Drupal::request()
      ->get('type');
    if (empty($typeLabel)) {
      $typeLabel = 'data';
    }
    switch ($typeData) {
      case 'table':
        $data = $this
          ->getTable($paragraph_field_items, $components, $setting);
        break;
      case 'data':
        $data = $this
          ->getData($paragraph_field_items, $components, $setting);
        break;
      default:
        $data = [
          $typeLabel => $this
            ->getResults($paragraph_field_items, $components, $setting),
        ];
        break;
    }
    return new JsonResponse($data);
  }
  else {
    $message[] = $this
      ->t("%field is not paragraphs", [
      "%field" => $field_name,
    ]);
  }
  return new JsonResponse([
    'error' => $message,
  ]);
}