EditController.php 4.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php

/**
 * @file
 * Contains of \Drupal\edit\EditController.
 */

namespace Drupal\edit;

use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Entity\EntityInterface;
use Drupal\edit\Ajax\FieldFormCommand;
use Drupal\edit\Ajax\FieldFormSavedCommand;
use Drupal\edit\Ajax\FieldFormValidationErrorsCommand;
19
use Drupal\edit\Ajax\MetadataCommand;
20
21
22
23
24
25
26
27
28
29
30
31
32

/**
 * Returns responses for Edit module routes.
 */
class EditController extends ContainerAware {

  /**
   * Returns the metadata for a set of fields.
   *
   * Given a list of field edit IDs as POST parameters, run access checks on the
   * entity and field level to determine whether the current user may edit them.
   * Also retrieves other metadata.
   *
33
34
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   The Ajax response.
35
36
   */
  public function metadata(Request $request) {
37
38
    $response = new AjaxResponse();

39
40
41
42
43
44
45
46
    $fields = $request->request->get('fields');
    if (!isset($fields)) {
      throw new NotFoundHttpException();
    }
    $metadataGenerator = $this->container->get('edit.metadata.generator');

    $metadata = array();
    foreach ($fields as $field) {
47
      list($entity_type, $entity_id, $field_name, $langcode, $view_mode) = explode('/', $field);
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

      // Load the entity.
      if (!$entity_type || !entity_get_info($entity_type)) {
        throw new NotFoundHttpException();
      }
      $entity = entity_load($entity_type, $entity_id);
      if (!$entity) {
        throw new NotFoundHttpException();
      }

      // Validate the field name and language.
      if (!$field_name || !($instance = field_info_instance($entity->entityType(), $field_name, $entity->bundle()))) {
        throw new NotFoundHttpException();
      }
      if (!$langcode || (field_valid_language($langcode) !== $langcode)) {
        throw new NotFoundHttpException();
      }

      $metadata[$field] = $metadataGenerator->generate($entity, $instance, $langcode, $view_mode);
    }

69
70
71
72
73
74
75
76
77
78
79
80
81
82
    $response->addCommand(new MetaDataCommand($metadata));

    // Determine in-place editors and ensure their attachments are loaded.
    $editors = array();
    foreach ($metadata as $edit_id => $field_metadata) {
      if (isset($field_metadata['editor'])) {
        $editors[] = $field_metadata['editor'];
      }
    }
    $editorSelector = $this->container->get('edit.editor.selector');
    $elements['#attached'] = $editorSelector->getEditorAttachments($editors);
    drupal_process_attached($elements);

    return $response;
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  }

  /**
   * Returns a single field edit form as an Ajax response.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity being edited.
   * @param string $field_name
   *   The name of the field that is being edited.
   * @param string $langcode
   *   The name of the language for which the field is being edited.
   * @param string $view_mode
   *   The view mode the field should be rerendered in.
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   The Ajax response.
   */
  public function fieldForm(EntityInterface $entity, $field_name, $langcode, $view_mode) {
    $response = new AjaxResponse();

    $form_state = array(
      'langcode' => $langcode,
      'no_redirect' => TRUE,
      'build_info' => array('args' => array($entity, $field_name)),
    );
    $form = drupal_build_form('edit_field_form', $form_state);

    if (!empty($form_state['executed'])) {
      // The form submission took care of saving the updated entity. Return the
      // updated view of the field.
112
      $entity = entity_load($form_state['entity']->entityType(), $form_state['entity']->id(), TRUE);
113
      $output = field_view_field($entity, $field_name, $view_mode, $langcode);
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

      $response->addCommand(new FieldFormSavedCommand(drupal_render($output)));
    }
    else {
      $response->addCommand(new FieldFormCommand(drupal_render($form)));

      $errors = form_get_errors();
      if (count($errors)) {
        $response->addCommand(new FieldFormValidationErrorsCommand(theme('status_messages')));
      }
    }

    // When working with a hidden form, we don't want any CSS or JS to be loaded.
    if (isset($_POST['nocssjs']) && $_POST['nocssjs'] === 'true') {
      drupal_static_reset('drupal_add_css');
      drupal_static_reset('drupal_add_js');
    }

    return $response;
  }

}