You are here

function ajax_form_entity_entity_view_alter in Ajax form entity 7

Same name and namespace in other branches
  1. 7.x ajax_form_entity.module \ajax_form_entity_entity_view_alter()

Implements hook_entity_view_alter().

File

./ajax_form_entity.module, line 424
Ajaxify entity forms.

Code

function ajax_form_entity_entity_view_alter(&$build, $type) {

  // It cannot be run without the bundle. @todo : check in which case it can occurs.
  if (isset($build['#bundle'])) {
    $bundle = $build['#bundle'];

    // If activated, process with the entity.
    $settings = variable_get('ajax_form_entity_' . $type . '_' . $bundle, array());
    if (isset($settings['activate']) && $settings['activate']) {
      switch ($type) {
        case 'user':
          $entity = $build['#account'];
          $entity_id = $build['#account']->{$settings['id']};
          break;
        case 'field_collection_item':
        case 'entityform':
          $entity = $build['#entity'];
          $entity_id = $build['#entity']->{$settings['id']};
          break;
        default:
          $entity = $build['#' . $type];
          $entity_id = $build['#' . $type]->{$settings['id']};
      }

      // Special ID for multiple form cases.
      $special_id = uniqid();
      drupal_add_library('system', 'drupal.ajax');

      // Special wrapper.
      if (isset($build['#prefix'])) {
        $build['#prefix'] = '<div id="ajax-entity-form-' . $special_id . '">' . $build['#prefix'];
      }
      else {
        $build['#prefix'] = '<div id="ajax-entity-form-' . $special_id . '">';
      }
      if (isset($build['#suffix'])) {
        $build['#suffix'] .= '</div><div id="ajax-entity-form-wrapper-' . $special_id . '"></div>';
      }
      else {
        $build['#suffix'] = '</div><div id="ajax-entity-form-wrapper-' . $special_id . '"></div>';
      }

      // If AJAX links are activated, add  them.
      $class = 'ajax-entity-form-' . str_replace('_', '-', $type);
      $class = ' ajax-entity-form-' . str_replace('_', '-', $type . '-' . $bundle);
      if (entity_access('update', $build['#entity_type'], $entity)) {
        if ($settings['edit_activate']) {
          $build['ajax_edit_link']['#markup'] = '<div id="ajax-entity-form-edit-' . $special_id . '" class="ajax-entity-form-edit ' . $class . '" >';
          $build['ajax_edit_link']['#markup'] .= l(t('Edit'), 'ajax-form-entity-edit/edit/nojs/' . $type . '/' . $entity_id . '/' . $special_id, array(
            'attributes' => array(
              'class' => array(
                'use-ajax edit-link',
              ),
            ),
          ));
          $build['ajax_edit_link']['#markup'] .= '</div>';
        }
      }
      if (entity_access('delete', $build['#entity_type'], $entity)) {
        if ($settings['delete_activate']) {

          // TODO : form instead of link for security reason. For now, sort of session to improve security. Not enough. @see ajax_form_entity_entity_delete_callback.
          $_SESSION['ajax_form_entity_delete'][$special_id] = TRUE . ($build['ajax_delete_link']['#markup'] = '<div id="ajax-entity-form-delete-' . $special_id . '" class="ajax-entity-form-delete ' . $class . '">');
          $build['ajax_delete_link']['#markup'] .= l(t('delete'), 'ajax-form-entity-delete/delete/nojs/' . $type . '/' . $entity_id . '/' . $special_id, array(
            'attributes' => array(
              'class' => array(
                'use-ajax delete-link',
              ),
            ),
          ));
          $build['ajax_delete_link']['#markup'] .= '</div>';
        }
      }
    }
  }
}