Hi,
I'm creating a custom layout/display with some ds fields.
What could be useful is a simplified way to add a custom field (maybe a dialog like for the dynamic field type?) without having to move to the create field and then back to display settings again and again.

It's just a suggestion, keep on with this great module.

Comments

muka’s picture

Here is some code, but need to be reviewed as it not works properly.
EDIT: missing redirection after field save

ds/modules/ds_extras/ds_extras.admin.inc ~ line 457

  /**
    Add "add field" links
  */
  $form['additional_settings']['add_custom_fields'] = array(
    '#type' => 'fieldset',
    '#group' => 'tabs',
    '#title' => t('Add custom fields'),
    '#weight' => 10,
  );

  $cfields = array();
  $ajaxconf = array(
    'callback' => 'ds_add_field_loadform',
    'wrapper' => 'ds-add-field-target',
    'method' => 'html',
    'effect' => 'fade',
  );

  $cfields['custom_field'] = array(
    '#type' => 'button',
    '#value' => t('Add a field'),
    '#name' => 'custom_field',
    '#weight' => 10,
    '#ajax' => $ajaxconf,
  );    
  // manage_ctools    
  $cfields['manage_ctools'] = array(
    '#type' => 'button',
    '#name' => 'manage_ctools',
    '#value' => t('Add a dynamic field'),
    '#weight' => 20,
    '#ajax' => $ajaxconf,
  );

  if (module_exists('block')) {
    $cfields['manage_block'] = array(
      '#type' => 'button',
      '#name' => 'manage_block',
      '#value' => t('Add a block field'),
      '#weight' => 30,
      '#ajax' => $ajaxconf,
    );
  }    

  $form['additional_settings']['add_custom_fields'] += $cfields;

  $form['additional_settings']['add_custom_fields']['content'] = array(
    '#markup' => "<div class='clear' id='ds-add-field-target'></div>",
    '#weight' => 50,
  );

  /** !eof Add "add field" links
  */

and the callback for ajax


/**
  * ds_add_field_saveform
  * ajax callback after field save callback
  *
  */

function ds_add_field_submit($form, $form_state)
{
//   $commands = array();
//   $commands[] = ajax_command_replace(NULL, "");
//   $commands[] = ajax_command_prepend(NULL, theme('status_messages'));
//   return array('#type' => 'ajax', '#commands' => $commands);

    $form_state['redirect'] = $form_state['storage']['ref'];
//     die("OK ".$form_state['storage']['ref']);
//     drupal_goto($form_state['storage']['ref']);

}

/**
  * ds_add_field_loadform
  * ajax callback to render add field form
  *
  */
// ds_add_field_loadform(null, null);
function ds_add_field_loadform($form, $form_state)
{

  // var_dump($form_state);die();

  switch($_POST['_triggering_element_name'])
  {
  case "manage_ctools":
    $form_name = "ds_edit_ctools_field_form";  
  break;
  case "manage_block":
    $form_name = "ds_edit_block_field_form";
  break;
  default:
    $form_name = "ds_edit_custom_field_form";  
  break;
  }

  module_load_include('inc','ds','ds.fields');
  module_load_include('inc','ds','ds.registry');

  $form = drupal_get_form($form_name);
  
  $menu_cb = _ds_menu();
  foreach($menu_cb as $path=>$mitem)
  {
    if(is_array($mitem['page arguments']) && $form_name == $mitem['page arguments'][0])
    {
      break;
    }
  }
  
  $form['#action'] = url($path);
  $form['#submit'] = array('ds_add_field_submit');
  
  $url = parse_url($_SERVER['HTTP_REFERER']);
//   $form_state['redirect'] = url(substr($url['path'],1));
  $form_state['storage']['ref'] = url(substr($url['path'],1));

  return drupal_render($form);
}
swentel’s picture

I've been thinking about this for a while as well, but I've always been afraid that adding a new row into the field ui would clutter up the interface, especially if people want to add new dynamic fields, block fields as well. I'd rather go for a solution with a dialog, like you also suggest. Just need to find a nice spot for that in the interface and see if I can dynamically update the field ui - which will be hard, so might be a refresh, but still, better that nothing.

- edit - the CTools wizard is probably a handy feature for this, I'll have a look at this next week.
- edit 2 - your technique is adding the form in the additional settings which is probably a good idea, I'll have a look at the code you posted and see if I can make it work.

swentel’s picture

StatusFileSize
new4.36 KB

Waauw, been playing with your code and it really is already freakingly cool :)
I've attached patch in progress in case other people want to start testing with this as well. Did some code cleanup (mostly code standards) and also added the preprocess field (new in dev). Patch applies to latest dev.

- edit -
* two things are missing:
- show errors in form (eg missing label, unique field)
- redirect after correct save

swentel’s picture

Status: Active » Needs work

changing status just to draw attention to other users maybe.

sun’s picture

+++ b/ds.field_ui.inc
@@ -1360,6 +1379,124 @@ function _ds_field_ui_fields($entity_type, $bundle, $view_mode, &$form, &$form_s
+function _ds_add_field_loadform($form, $form_state) {
...
+  module_load_include('inc','ds','ds.fields');
+  module_load_include('inc','ds','ds.registry');

You have to use http://api.drupal.org/api/drupal/includes--form.inc/function/form_load_i... here instead.

Don't forget to take &$form_state by reference.

muka’s picture

StatusFileSize
new6.74 KB

Hi, I've moved the code to a ctools modal avoiding some strangeness in the form building.
I've got two points of doubt in the code, in line 175 and 200 in the patch attached.

* I use $_POST instead $form_state, I can't find how to get it
* Field UI regeneration creates a form_id--N which break the js functionality; still a form_build problem I think

Patch is built upon the latest 7.x-1.x dev branch.

Thanks in advance.

swentel’s picture

StatusFileSize
new5.64 KB

Cool stuff. Updated patch attached with simplied code (on latest dev).
Instead of calling the form after the field save, I'm triggering a ds javascript event which now reloads the page. This is the only thing left which is not 'that' nice. If we could somehow trigger the #edit-refresh button with a row to refresh the table with ajax than we're there. I'll ping yched about this to see if he can shed some light on this. Other than that, this patch is 99% good to go.

swentel’s picture

Status: Needs work » Fixed

Committed this. A field save triggers the save button so any other changes in the table are saved as well and the new field will show up too.

muka’s picture

StatusFileSize
new475 bytes

Thanks!
Just in case, I've added a raw refresh $.get-ing the same document.location and reattaching behaviors.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.