Commit 656a4d91 authored by Josh Caldwell's avatar Josh Caldwell
Browse files

followup on 1419314, make entity boxes be able to be created and edited from the blocks screens

parent a35e36d4
Loading
Loading
Loading
Loading
+139 −38
Changes for entity_boxes.admin.inc: 139 added lines, 38 removed lines.
Original line number Diff line number Diff line
@@ -20,11 +20,19 @@ class BoxEntityUIController extends EntityDefaultUIController {
   * We are simply using the boxes forms to create entities.
   */
  public function hook_menu() {
    
    $items = array();
    $id_count = count(explode('/', $this->path));
    $wildcard = isset($this->entityInfo['admin ui']['menu wildcard']) ? $this->entityInfo['admin ui']['menu wildcard'] : '%' . $this->entityType;

    $items['entity_boxes/getform/%'] = array (
      'title' => 'FORMS TESTER',
      'page callback' => 'entity_boxes_form_wrapper',
      'page arguments' => array(2),
      'access callback' => 'entity_boxes_access',
      'access arguments' => array('view', 1),
      'type' => MENU_CALLBACK,
    );

    // Menu item for viewing entity_boxes
    $items['entity_boxes/' . $wildcard] = array(
      //'title' => 'Title',
@@ -67,9 +75,9 @@ class BoxEntityUIController extends EntityDefaultUIController {
 *
 * @see entity_boxes_edit_form()
 */
function entity_boxes_form_wrapper($entity_boxes) {
function entity_boxes_form_wrapper($delta) {
  // Add the breadcrumb for the form's location.
  entity_boxes_set_breadcrumb();
  $entity_boxes = entity_boxes_load_delta($delta);
  return drupal_get_form('entity_boxes_edit_form', $entity_boxes);
}

@@ -97,47 +105,144 @@ function entity_boxes_delete_form_wrapper($entity_boxes) {
 *     with only a entity_boxes type defined.
 */
function entity_boxes_edit_form($form, &$form_state, $entity_boxes = FALSE) {
  // Add the field related form elements.
  $form = array();
  //short out if we didn't receive any entity_boxes
  if(!$entity_boxes) {
    $entity_boxes = (!is_array($entity_boxes) || !$entity_boxes) ?
        entity_boxes_create(array('type' => $form_state['box']->plugin_key)) : array_pop($entity_boxes);
  }

  if(is_array($entity_boxes)) {
    $entity_boxes = current($entity_boxes);
  }

  $form_state['entity_boxes'] = $entity_boxes;
  field_attach_form('entity_boxes', $entity_boxes, $form, $form_state);

  $form['actions'] = array(
    '#type' => 'container',
    '#attributes' => array('class' => array('form-actions')),
    '#weight' => 400,
  $form['entity_boxes_id'] = array(
    '#type' => 'value',
    '#value' => $entity_boxes->entity_boxes_id,
  );

  // We add the form's #submit array to this button along with the actual submit
  // handler to preserve any submit handlers added by a form callback_wrapper.
  $submit = array();

  if (!empty($form['#submit'])) {
    $submit += $form['#submit'];
  }
  $form['name'] = array(
    '#type' => 'value',
    '#value' => $form_state['box']->delta,
  );

  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save @entity_boxes', array('@entity_boxes' => !empty($entity_boxes->description) ? $entity_boxes->description : '')),
    '#submit' => $submit + array('entity_boxes_edit_form_submit'),
  $form['description'] = array(
    '#type' => 'textfield',
    '#title' => t('Box description'),
    '#default_value' => $form_state['box']->description,
    '#maxlength' => 64,
    '#description' => t('A brief description of your box.'),
    '#required' => TRUE,
  );

  if (!empty($entity_boxes->name)) {
    $cancel_path = (path_is_admin($_GET['q'])) ? 'admin/content/entity_boxes' : $_GET['q'];

    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete @entity_boxes', array('@entity_boxes' => !empty($entity_boxes->description) ? $entity_boxes->description : '')),
      '#suffix' => l(t('Cancel'), $cancel_path),
      '#submit' => $submit + array('entity_boxes_form_submit_delete'),
      '#submit' => array('entity_boxes_form_submit_delete'),
      '#weight' => 45,
    );
  }
  
  // We append the validate handler to #validate in case a form callback_wrapper
  // is used to add validate handlers earlier.
  $form['#validate'][] = 'entity_boxes_edit_form_validate';
  //store our state in the form so it can be accessed on the block pages
  $form['form_state_info'] = array(
    '#type' => 'value',
    '#value' => $form_state,
  );

   
  $box = $form_state['box'];
  
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#attributes' => array('class' => array('boxes-ajax', 'use-ajax-submit')),
    '#weight' => 25,
  );
  $form['cancel'] = array(
    '#type' => 'submit',
    '#value' => t('Cancel'),
    '#limit_validation_errors' => array(),
    '#attributes' => array('class' => array('boxes-ajax', 'use-ajax-submit')),
    '#weight' => 26,
  );

  if (!empty($form_state['custom_action'])) {
    $form['#action'] = url($_GET['q'], array('query' => array(
        'boxes_delta' => $box->delta,
        'plugin_key' => $form_state['plugin_key'],
      )));
  }
  if (isset($form_state['init_form']) && $box->use_multistep_create()) {
    unset($form['options']);
    unset($form['cancel']);
    $form['submit']['#value'] = t('Continue');
    if (!empty($form_state['custom_action'])) {
      $form['#action'] = url($_GET['q'], array(
        'query' => array(
          'boxes_delta' => $box->delta,
          'init_form_continue' => TRUE,
          'plugin_key' => $form_state['plugin_key'],
        )
      ));
    }
    return $form;
  }

  $form['boxes_adv'] = array(
    '#type'=> 'fieldset',
    '#collapsed' => TRUE,
    '#collapsible' => TRUE,
    '#title' => t('Advanced Settings'),
    '#weight' => 24,
  );
  $form['boxes_adv']['additional_classes'] = array(
    '#title' => t('Additional CSS classes'),
    '#description' => t('Optional CSS classes that will be added to the top-level div container for this box. Separate them with spaces.'),
    '#type' => 'textfield',
    '#default_value' => isset($box->options['additional_classes']) ? $box->options['additional_classes'] : '',
  );
  return $form;
}

/**
 *  Implementation of hook_form_FORM_ID_alter().
 *  Add our form state back so fields knows what is going on.
 *  This gets lost because hook_block_configure doesn't accept form_state
 */
function entity_boxes_form_alter (&$form, &$form_state, $form_id) {
  // Break out if its not a boxes for, this isn't an entity box or we're at the beginning
  // of a 2 part form
  if (!in_array($form_id, array('block_admin_configure', 'boxes_add_form', 'boxes_box_form') ||
      !(isset($form['settings']['entity_boxes_id']) || isset($form['entity_boxes_id'])) ||
      (isset($form_state['init_form']) && $form_state['box']->use_multistep_create())  
  ) {
    return;
  }
  //move our formstate over from the form itself (this is for bypassing hook_block_configure)
  if (isset($form['settings']['form_state_info']['#value'])) {
    $form_state += $form['settings']['form_state_info']['#value'];
    unset($form['settings']['form_state_info']);
  } else {
    unset($form['form_state_info']);
  }

  $entity_boxes = $form_state['entity_boxes'];
  //figure out which part of the form to attach the fields to
  if ($form_id == 'boxes_add_form' || $form_id == 'boxes_box_form') {
    $portion = &$form;
  } else {
    $portion = &$form['settings'];
    unset($form['settings']['actions']);
  }
  //attach our fields to the proper part of the form
  field_attach_form('entity_boxes', $entity_boxes, $portion, $form_state);
}

/**
 * Form API validate callback for the entity_boxes form
@@ -152,23 +257,19 @@ function entity_boxes_edit_form_validate(&$form, &$form_state) {

/**
 * Form API submit callback for the entity_boxes form.
 * 
 * @todo remove hard-coded link
 */
function entity_boxes_edit_form_submit(&$form, &$form_state) {
  
  $entity_boxes = entity_ui_controller('entity_boxes')->entityFormSubmitBuildEntity($form, $form_state);
  // Save the entity_boxes and go back to the list of entity_boxes
  
  // Add in created and changed times.
  if ($entity_boxes->is_new = isset($entity_boxes->is_new) ? $entity_boxes->is_new : 0) {
    $entity_boxes->created = time();
  if (empty($form_state['entity_boxes']) && !empty($form_state['values']['entity_boxes_id'])) {
    $id = $form_state['values']['entity_boxes_id'];
    $form_state['entity_boxes'] = entity_boxes_load($id);
  }

  $entity_boxes->changed = time();
  if(isset($form_state['delta'])) {
    $form_state['name'] = $form_state['delta'];
  }

  $entity_boxes->save();
  $form_state['redirect'] = 'admin/content/entity_boxes';
  $entity_boxes = entity_ui_controller('entity_boxes')->entityFormSubmitBuildEntity($form, $form_state);
  return entity_boxes_save($entity_boxes);
}

/**
@@ -177,10 +278,10 @@ function entity_boxes_edit_form_submit(&$form, &$form_state) {
 * @todo Remove hard-coded path
 */
function entity_boxes_form_submit_delete(&$form, &$form_state) {
  $form_state['redirect'] = 'admin/content/entity_boxes/entity_boxes/' . $form_state['entity_boxes']->entity_boxes_id . '/delete';
  $entity_boxes = $form_state['entity_boxes'];
  return entity_boxes_delete($entity_boxes);
}


/**
 * Form callback: confirmation form for deleting a entity_boxes.
 *
+11 −1
Changes for entity_boxes.module: 11 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -295,7 +295,17 @@ function entity_boxes_create($values = array()) {
 *   The entity_boxes object.
 */
function entity_boxes_save(BoxEntity $entity_boxes) {
  return $entity_boxes->save();
  $entity_boxes->is_new = empty($entity_boxes->entity_boxes_id);

  // Add in created and changed times.
  if ($entity_boxes->is_new) {
    $entity_boxes->created = time();
  } 
  $entity_boxes->changed = time();
  
  $entity_boxes->save();

  return $entity_boxes;
}


+0 −2
Changes for entity_boxes_type.admin.inc: 0 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -25,7 +25,6 @@ class BoxEntityTypeUIController extends EntityDefaultUIController {
 * Generates the entity_boxes type editing form.
 */
function entity_boxes_type_form($form, &$form_state, $entity_boxes_type, $op = 'edit') {

  if ($op == 'clone') {
    $entity_boxes_type->label .= ' (cloned)';
    $entity_boxes_type->type = '';
@@ -44,7 +43,6 @@ function entity_boxes_type_form($form, &$form_state, $entity_boxes_type, $op = '
    '#type' => 'machine_name',
    '#default_value' => isset($entity_boxes_type->type) ? $entity_boxes_type->type : '',
    '#maxlength' => 32,
//    '#disabled' => $entity_boxes_type->isLocked() && $op != 'clone',
    '#machine_name' => array(
      'exists' => 'entity_boxes_get_types',
      'source' => array('label'),
+34 −75
Changes for plugins/entity_boxes_entity.inc: 34 added lines, 75 removed lines.
Original line number Diff line number Diff line
@@ -15,14 +15,6 @@ class entity_boxes_entity extends boxes_box {
  public function options_defaults() {
    return array();
  }
  /**
   * implements boxes_box::option_submit()
   * This needs a boxes patch
   */
  public function options_submit($form, &$form_state) {
    return $form;
  }


  /**
   * we use fancy ajax on our options form and this breaks with the context ui
@@ -43,94 +35,61 @@ class entity_boxes_entity extends boxes_box {

  /**
   *  Implementation of boxes_box::options_form().
   *
   *  @see boxes_form instead
   */
  public function options_form(&$form_state) {
    //this is necessary so that boxes will know there is a form
   return TRUE;
  }
  /**
   *  This function will override the default options form settings.
   *  We need this to add our submit (which saves the entity part too)
   *  @see boxes_box_form()
   */
  public function box_form($form, &$form_state) {
    
    drupal_add_js(drupal_get_path('module', 'entity_boxes') . '/entity_boxes.js');

    $box = $form_state['box'];
    $form['plugin_key'] = array(
      '#type' => 'value',
      '#value' => $box->plugin_key,
    );
    $form['delta'] = array(
      '#type' => 'value',
      '#value' => $box->delta,
    );
    $form['name'] = array(
      '#type' => 'value',
      '#value' => $box->delta,
    );
    $form['description'] = array(
      '#type' => 'textfield',
      '#title' => t('Box description'),
      '#default_value' => $box->description,
      '#maxlength' => 64,
      '#description' => t('A brief description of your box.'),
      '#required' => TRUE,
      '#weight' => -19,
    );
    $entity_boxes = entity_boxes_load_delta($form_state['box']->delta);
    $form = entity_boxes_edit_form($form, $form_state, $entity_boxes);
    
    //add in the information boxes needs to save this
    $entity_boxes = $form_state['entity_boxes'];
    $box = reset($entity_boxes);
    
    $form['title'] = array(
      '#type' => 'textfield',
      '#title' => t('Box title'),
      '#maxlength' => 64,
      '#description' => t('The title of the box as shown to the user.'),
      '#default_value' => $box->title,
      '#description' => t('The rendered title of the box as shown to the user.'),
      '#default_value' => $form_state['box']->title,
      '#weight' => -18,
    );

    if (isset($form_state['init_form']) && $box->use_multistep_create()) {
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Continue'),
        '#attributes' => array('class' => array('boxes-ajax', 'use-ajax-submit')),
    $form['plugin_key'] = array(
      '#type' => 'value',
      '#value' => $form_state['box']->plugin_key,
    );
    $form['delta'] = array(
      '#type' => 'value',
      '#value' => $form_state['box']->delta,
    );
      if (!empty($form_state['custom_action'])) {
        $form['#action'] = url($_GET['q'], array(
          'query' => array(
            'boxes_delta' => $box->delta,
            'init_form_continue' => TRUE,
            'plugin_key' => $form_state['plugin_key'],
          )
        ));
      }
      return $form;
    }
        
    $entity_boxes = entity_boxes_load_delta($box->delta);
    
    $entity_boxes = (!is_array($entity_boxes) || !$entity_boxes) ?
      entity_boxes_create(array('type' => $box->plugin_key)) : array_pop($entity_boxes);    
    
    module_load_include("inc", "entity_boxes", "entity_boxes.admin");
    $form = entity_boxes_edit_form($form, $form_state, $entity_boxes);
    
    // The redirect behavior is dependent on whether we are using the AJAX version or the admin interface
    if (stripos(request_uri(), 'admin/structure/block') > 0) {
      $form_state['redirect'] = 'admin/structure/block';
    }
    else {
      $form['actions']['submit']['#attributes'] = array('class' => array('boxes-ajax', 'use-ajax-submit'));
    }
    $form['actions']['submit']['#submit'][] ='boxes_box_form_submit'; 
    
    //set the action to not include the continue (since we're done)
    $form['#action'] = url($_GET['q'], array(
      'query' => array(
        'boxes_delta' => $box->delta,
        'plugin_key' => $box->plugin_key,
      )
    ));
    
    return $form;
  }

  public function control_form() {
    return TRUE;
  }

  /**
   *  Implements Boxes_Box::options_submit().
   *  Allows us to save our extra data that boxes won't know about
   */
  public function options_submit(&$form, &$form_state) {
    $form_state['values']['name'] = $form_state['values']['delta'];
    entity_boxes_edit_form_submit($form, $form_state);
    return $form;
  }

  /**
   * Implementation of boxes_box::render().