You are here

function quickedit_metadata in Quick Edit 7

Page callback: Returns the metadata for a set of fields.

Given a list of field quickedit 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.

Return value

The JSON response.

See also

Drupal 8's QuickEditController::metadata()

QuickEditMetadataGenerator

EditEntityFieldAccessCheck

QuickEditEditorSelector

1 string reference to 'quickedit_metadata'
quickedit_menu in ./quickedit.module
Implements hook_menu().

File

includes/pages.inc, line 22
AJAX endpoint to retrieve & save subforms for fields and re-render fields.

Code

function quickedit_metadata() {
  $fields = $_POST['fields'];
  if (!isset($fields)) {
    return MENU_NOT_FOUND;
  }
  $entities = isset($_POST['entities']) ? $_POST['entities'] : array();
  module_load_include('php', 'quickedit', 'includes/EditEntityFieldAccessCheck');
  module_load_include('php', 'quickedit', 'includes/QuickEditEditorSelector');
  module_load_include('php', 'quickedit', 'includes/QuickEditMetadataGenerator');
  $accessChecker = new EditEntityFieldAccessCheck();
  $editorSelector = new QuickEditEditorSelector();
  $metadataGenerator = new QuickEditMetadataGenerator($accessChecker, $editorSelector);

  // Build metadata for each field, track all in-place editors.
  $metadata = array();
  $editors = array();
  foreach ($fields as $field) {
    list($entity_type, $entity_id, $field_name, $langcode, $view_mode) = explode('/', $field);

    // Load the entity.
    if (!$entity_type || !entity_get_info($entity_type)) {
      return MENU_NOT_FOUND;
    }
    $entity = entity_load_single($entity_type, $entity_id);
    if (!$entity) {
      return MENU_NOT_FOUND;
    }
    list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);

    // Validate the field name and language.
    if (!_quickedit_is_extra_field($entity_type, $field_name)) {
      if (!$field_name || !($instance = field_info_instance($entity_type, $field_name, $bundle))) {
        return MENU_NOT_FOUND;
      }
    }
    else {
      $instance = array(
        'field_name' => $field_name,
      );
    }
    if (!$langcode || field_valid_language($langcode) !== $langcode) {
      return MENU_NOT_FOUND;
    }

    // If the entity information for this field is requested, include it.
    $entity_id = $entity_type . '/' . $entity_id;
    if (is_array($entities) && in_array($entity_id, $entities) && !isset($metadata[$entity_id])) {
      $metadata[$entity_id] = $metadataGenerator
        ->generateEntityMetadata($entity_type, $entity, $langcode);
    }

    // Generate metadata for the current field.
    $metadata[$field] = $metadataGenerator
      ->generateFieldMetadata($entity_type, $entity, $instance, $langcode, $view_mode);

    // Track all editors.
    if ($metadata[$field]['access'] !== FALSE) {
      $editors[] = $metadata[$field]['editor'];
    }
  }
  drupal_json_output($metadata);
}