You are here

function paragraphs_field_instance_settings_form in Paragraphs 7

Implements hook_field_instance_settings_form().

File

./paragraphs.module, line 441
Paragraphs hooks and common functions.

Code

function paragraphs_field_instance_settings_form($field, $instance) {
  $settings = $instance['settings'];
  $bundles = array();
  $_bundles = paragraphs_bundle_load();
  $form_delta = count($_bundles) * 2;
  $max_weight = 0;
  $weights = array();
  foreach ($_bundles as $machine_name => $bundle) {
    $bundles[$machine_name] = $bundle->name;
    if (isset($settings['bundle_weights'][$machine_name])) {
      $weights[$machine_name] = $settings['bundle_weights'][$machine_name];
      if ($settings['bundle_weights'][$machine_name] > $max_weight) {
        $max_weight = $settings['bundle_weights'][$machine_name];
      }
    }
  }
  $max_weight++;
  $element['allowed_bundles_table'] = array(
    '#tree' => TRUE,
    '#prefix' => '<label>' . t('Allowed Paragraph bundles') . '</label>',
    '#suffix' => '<div class="description">' . t('If no bundle is selected, all the bundles will be available.') . '</div>',
  );
  $weight = 1;
  foreach ($_bundles as $machine_name => $bundle) {
    $enabled = FALSE;
    if (isset($settings['allowed_bundles'][$machine_name]) && $settings['allowed_bundles'][$machine_name] === $machine_name) {
      $enabled = TRUE;
    }
    $element['allowed_bundles_table'][$machine_name] = array(
      'enabled' => array(
        '#type' => 'checkbox',
        '#title' => check_plain($bundle->name) . ' <em>(' . check_plain($bundle->label) . ')</em>',
        '#title_display' => 'after',
        '#default_value' => $enabled,
      ),
      'weight' => array(
        '#type' => 'weight',
        '#title' => t('Weight'),
        '#default_value' => isset($weights[$machine_name]) ? $weights[$machine_name] : $weight + $max_weight,
        '#delta' => $form_delta,
        '#title_display' => 'invisible',
      ),
    );
    $element['allowed_bundles_table'][$machine_name]['#weight'] = $element['allowed_bundles_table'][$machine_name]['weight']['#default_value'];
    $weight++;
  }
  $element['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Item Title'),
    '#description' => t('Label to appear as title on the button as "Add new [title]", this label is translatable'),
    '#default_value' => isset($settings['title']) ? $settings['title'] : PARAGRAPHS_DEFAULT_TITLE,
    '#required' => TRUE,
  );
  $element['title_multiple'] = array(
    '#type' => 'textfield',
    '#title' => t('Plural Item Title'),
    '#description' => t('Title in its plural form.'),
    '#default_value' => isset($settings['title_multiple']) ? $settings['title_multiple'] : PARAGRAPHS_DEFAULT_TITLE_MULTIPLE,
    '#required' => TRUE,
  );
  $element['default_edit_mode'] = array(
    '#type' => 'select',
    '#title' => t('Default edit mode'),
    '#description' => t('The default edit mode the paragraph item is in. Preview will render the paragraph in the preview view mode.'),
    '#options' => array(
      'open' => t('Open'),
      'closed' => t('Closed'),
      'preview' => t('Preview'),
    ),
    '#default_value' => isset($settings['default_edit_mode']) ? $settings['default_edit_mode'] : PARAGRAPHS_DEFAULT_EDIT_MODE,
    '#required' => TRUE,
  );
  $elements_exists = module_exists('elements');
  $element['default_edit_mode_override'] = array(
    // If the Elements module is installed then use a real number field.
    '#type' => $elements_exists ? 'numberfield' : 'textfield',
    '#title' => t('Edit mode override'),
    '#title_display' => 'invisible',
    '#field_prefix' => t('Force items to display as "Open" when there are less then'),
    '#field_suffix' => t('items.'),
    '#default_value' => isset($settings['default_edit_mode_override']) && !empty($settings['default_edit_mode_override']) ? $settings['default_edit_mode_override'] : PARAGRAPHS_DEFAULT_EDIT_MODE_OVERRIDE,
    '#states' => array(
      // Hide the settings when the the default edit mode is set to "Open.".
      'invisible' => array(
        ':input[name="instance[settings][default_edit_mode]"]' => array(
          'value' => 'open',
        ),
      ),
    ),
    '#size' => 2,
    '#maxlength' => 2,
    // Specify width, since size property isn't supported on number fields.
    '#attributes' => $elements_exists ? array(
      'style' => 'width:3em',
    ) : array(),
    '#min' => 1,
    '#max' => 99,
  );
  if (!$elements_exists) {
    $element['default_edit_mode_override']['#element_validate'] = array(
      'element_validate_integer_positive',
    );
  }
  $element['add_mode'] = array(
    '#type' => 'select',
    '#title' => t('Add mode'),
    '#description' => t('The way to add new paragraphs.'),
    '#options' => array(
      'select' => t('Select List'),
      'button' => t('Buttons'),
    ),
    '#default_value' => isset($settings['add_mode']) ? $settings['add_mode'] : PARAGRAPHS_DEFAULT_ADD_MODE,
    '#required' => TRUE,
  );
  if (!count($bundles)) {
    $link = l(t('here'), 'admin/structure/paragraphs/add', array(
      'query' => drupal_get_destination(),
    ));
    $element['allowed_bundles_explain'] = array(
      '#type' => 'markup',
      '#markup' => t('You did not add any paragraph bundles yet, click !here to add one.', array(
        '!here' => $link,
      )),
    );
  }
  $element['fieldset'] = array(
    '#type' => 'fieldset',
    '#title' => t('Default value'),
    '#collapsible' => FALSE,
    // As field_ui_default_value_widget() does, we change the #parents so that
    // the value below is writing to $instance in the right location.
    '#parents' => array(
      'instance',
    ),
  );

  // Be sure to set the default value to NULL, for example to repair old fields
  // that still have one.
  $element['fieldset']['default_value'] = array(
    '#type' => 'value',
    '#value' => NULL,
  );
  $element['fieldset']['content'] = array(
    '#pre' => '<p>',
    '#markup' => t('To specify a default value, configure it via the regular default value setting of each field that is part of the paragraph bundle. To do so, go to the <a href="!url">Manage fields</a> screen of the paragraph bundle.', array(
      '!url' => url('admin/structure/paragraphs'),
    )),
    '#suffix' => '</p>',
  );
  return $element;
}