Using multiple settings forms for shortcodes doesn't work in the WYSIWYG integration. I've created two shortcodes with both their own settings form callback.
However, both forms get rendered below each other. I've tried implementing #states logic, but this doesn't work either (because both forms have a 'title' element).

The only option I guess would be to load the belonging form using an AJAX callback.

Comments

denes.szabo’s picture

Can you show to me your code?

BarisW’s picture

Sure:

<?php
function MYMODULE_shortcode_info() {
  $shortcodes['box'] = array(
    'title' => t('Box'),
    'description' => t('Place a colored box next to the text.'),
    'process callback' => 'MYMODULE_shortcode_box',
    'tips callback' => 'MYMODULE_shortcode_box_tip',
    'attributes callback' => 'MYMODULE_shortcode_box_settings'
  );

  $shortcodes['tile'] = array(
    'title' => t('Tile'),
    'description' => t('Insert a large clickable area.'),
    'process callback' => 'MYMODULE_shortcode_tile',
    'tips callback' => 'MYMODULE_shortcode_tile_tip',
    'attributes callback' => 'MYMODULE_shortcode_tile_settings'
  );
  return $shortcodes;
}

/**
 * Settings form for the 'box' shortcode.
 */
function MYMODULE_shortcode_box_settings(){
  return array(
    'title' => array(
      '#title' => t('Title'),
      '#type' => 'textfield',
    ),
    'color' => array(
      '#title' => t('Background color'),
      '#type' => 'select',
      '#options' => drupal_map_assoc(array('dark', 'light')),
      '#default_value' => 'dark',
    ),
    'align' => array(
      '#title' => t('Alignment'),
      '#type' => 'select',
      '#options' => drupal_map_assoc(array('left', 'right')),
      '#default_value' => 'right',
    ),
  );
}

/**
 * Settings form for the 'tile' shortcode.
 */
function MYMODULE_shortcode_tile_settings(){
  return array(
    'title' => array(
      '#title' => t('Title'),
      '#type' => 'textfield',
    ),
    'link' => array(
      '#title' => t('Link URL'),
      '#type' => 'textfield',
      '#attributes' => array(
        'placeholder' => t('http://'),
      ),
    ),
    'width' => array(
      '#title' => t('Display width'),
      '#type' => 'select',
      '#options' => drupal_map_assoc(array('half', 'full')),
      '#default_value' => 'half',
    ),
  );
}

?>
jurriaanroelofs’s picture

If you use states like in the sc_basic module you can have multiple shortcodes in one form but only show form elements related to 1 specific shortcode.

See the demo to get an idea of how this works:
http://youtu.be/IvA3SYPmqVo?t=13s

The key is to make any shortcode attribute only visible when its corresponding selectbox option is selected. This is accomplished with the form states:

function sc_basic_button_attributes($form, $form_state) {
  $form['url'] = array(
    '#type' => 'textfield',
    '#title' => t('Link url'),
    '#size' => 30,
    '#maxlength' => 30,
    '#states' => array(
      'visible' => array(
        ':input[name="shortcode"]' => array('value' => 'button'),
      ),
    ),
  );

More examples: http://drupalcode.org/project/sc_basic.git/blob/refs/heads/7.x-1.x:/sc_b...

BarisW’s picture

Tried, but doesn't work as expected. The problem is that each form can have fields with the same title (eg a Title field).
Using #states only hides elements but the form still contains all these elements.

denes.szabo’s picture

Issue summary: View changes

Moved to the shortcode_wysiwyg project.

karens’s picture

Confirming that the states system does not handle this correctly. I just created a patch to populate the attributes that come with this module, which makes it clear that it does not work. So I changed it to prefix each attribute with the shortcode name so each is unique, which will work correctly to show/hide elements.

See https://www.drupal.org/node/2312353.

The only other way to solve the problem, as noted above, would be to change the system completely and ajax load forms based on the shortcode selection. That would be a lot more work, so I assume this other solution makes more sense.

denes.szabo’s picture

Status: Active » Fixed

KarenS patches applied to the latest dev version.

Status: Fixed » Closed (fixed)

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