You are here

public function MefibsDisplayExtender::buildOptionsForm in MEFIBS - More exposed forms in blocks 8

Provide a form to edit options for this plugin.

Overrides DisplayExtenderPluginBase::buildOptionsForm

File

lib/Drupal/mefibs/Plugin/views/display_extender/MefibsDisplayExtender.php, line 59
Contains \Drupal\mefibs\Plugin\views\display_extender\MefibsDisplayExtender.

Class

MefibsDisplayExtender
Plugin annotation @ViewsDisplayExtender( id = "mefibs", title = @Translation("Mefibs"), help = @Translation("Provides additional exposed filter blocks for this view."), no_ui = FALSE )

Namespace

Drupal\mefibs\Plugin\views\display_extender

Code

public function buildOptionsForm(&$form, &$form_state) {
  if ($form_state['type'] != 'mefibs') {
    return;
  }
  $view = $form_state['view'];
  $display_id = $form_state['display_id'];

  // Add javascript needed for ajax form behaviors.
  drupal_add_js(drupal_get_path('module', 'mefibs') . '/js/mefibs_admin.js');
  drupal_add_css(drupal_get_path('module', 'mefibs') . '/css/mefibs_admin.css');

  // Get the settings.
  $settings = $this->displayHandler
    ->getOption('mefibs');
  $blocks = $settings['blocks'];

  // Find out if we are in an edit context here.
  $edit = FALSE;
  if (isset($form_state['storage']['action']) && $form_state['storage']['action'] == 'edit') {
    $edit = $form_state['storage']['id'];
  }
  if (isset($view->form_cache['blocks'])) {
    $blocks = $view->form_cache['blocks'];
    if ($edit === FALSE && $blocks != $settings['blocks']) {
      drupal_set_message('* ' . t('All changes are stored temporarily. Click Apply to make your changes permanent. Click Cancel to discard your changes.'), 'warning', FALSE);
    }
  }
  $form['mefibs'] = array(
    '#prefix' => '<div id="mefibs-display-extender-blocks-wrapper-outer">',
    '#suffix' => '</div>',
    '#blocks' => $blocks,
    '#tree' => TRUE,
  );
  $form['#title'] .= t('Enable additional blocks for exposed elements');
  $form['mefibs']['description'] = array(
    '#type' => 'markup',
    '#markup' => t('Choose which blocks are available for exposed input fields for this display.'),
    '#prefix' => '<div class="description">',
    '#suffix' => '</div>',
  );
  $form['mefibs']['blocks'] = array(
    '#prefix' => '<div id="mefibs-display-extender-blocks-wrapper">',
    '#suffix' => '</div>',
    '#theme' => 'mefibs_views_ui_block_list',
    '#view' => $this->view,
  );
  for ($id = 0; $id < count($blocks); $id++) {
    if ($edit !== FALSE && $edit === $id) {
      $form['mefibs']['blocks'][$id]['name'] = array(
        '#type' => 'textfield',
        '#default_value' => $blocks[$id]['name'],
        '#size' => 12,
      );
      $form['mefibs']['blocks'][$id]['machine_name'] = array(
        '#type' => 'machine_name',
        '#machine_name' => array(
          'label' => t('Machine name'),
          'source' => array(
            'options',
            'mefibs',
            'blocks',
            $id,
            'name',
          ),
          'exists' => 'mefibs_block_machine_name_exists',
        ),
        '#required' => FALSE,
        '#default_value' => $blocks[$id]['machine_name'],
        '#size' => 12,
      );
    }
    else {
      $form['mefibs']['blocks'][$id]['name'] = array(
        '#type' => 'markup',
        '#markup' => $blocks[$id]['name'],
      );
      $form['mefibs']['blocks'][$id]['machine_name'] = array(
        '#type' => 'markup',
        '#markup' => $blocks[$id]['machine_name'],
      );
    }
    $items = mefibs_get_expected_items_for_exposed_form_block($this->view, $blocks[$id]['machine_name']);
    $form['mefibs']['blocks'][$id]['filters'] = array(
      '#type' => 'markup',
      '#markup' => count($items['filter']),
      '#access' => $edit !== $id,
    );
    $form['mefibs']['blocks'][$id]['sorts'] = array(
      '#type' => 'markup',
      '#markup' => count($items['sort']),
      '#access' => $edit !== $id,
    );
    $actions = array(
      'edit' => t('Edit'),
      'save' => t('Save'),
      'cancel' => t('Cancel'),
      'remove' => t('Remove'),
    );
    foreach ($actions as $type => $label) {
      $form['mefibs']['blocks'][$id]['actions'][$type] = array(
        '#type' => 'submit',
        '#default_value' => $label,
        '#name' => 'edit-mefibs-block-action-' . $type . '-' . $id,
        '#group' => $id,
        '#access' => $edit === FALSE || $edit === $id,
        '#ajax' => array(
          'path' => current_path(),
        ),
      );
    }
    if ($edit !== FALSE && $edit === $id) {
      $form['mefibs']['blocks'][$id]['actions']['edit']['#prefix'] = '<div style="display: none;">';
      $form['mefibs']['blocks'][$id]['actions']['edit']['#suffix'] = '</div>';
      $form['mefibs']['blocks'][$id]['actions']['edit']['#weight'] = 100;
      $form['mefibs']['blocks'][$id]['actions']['remove']['#prefix'] = '<div style="display: none;">';
      $form['mefibs']['blocks'][$id]['actions']['remove']['#suffix'] = '</div>';
      $form['mefibs']['blocks'][$id]['actions']['remove']['#weight'] = 100;
    }
    else {
      $form['mefibs']['blocks'][$id]['actions']['save']['#prefix'] = '<div style="display: none;">';
      $form['mefibs']['blocks'][$id]['actions']['save']['#suffix'] = '</div>';
      $form['mefibs']['blocks'][$id]['actions']['save']['#weight'] = 100;
      $form['mefibs']['blocks'][$id]['actions']['cancel']['#prefix'] = '<div style="display: none;">';
      $form['mefibs']['blocks'][$id]['actions']['cancel']['#suffix'] = '</div>';
      $form['mefibs']['blocks'][$id]['actions']['cancel']['#weight'] = 100;
    }
  }

  // The "add new block".
  $form['mefibs']['add_block'] = array(
    '#type' => 'fieldset',
    '#title' => t('Add new block'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['mefibs']['add_block']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('New block'),
    '#default_value' => '',
    '#size' => 20,
  );
  $form['mefibs']['add_block']['machine_name'] = array(
    '#type' => 'machine_name',
    '#machine_name' => array(
      'label' => t('Machine name'),
      'source' => array(
        'options',
        'mefibs',
        'add_block',
        'name',
      ),
      'exists' => 'mefibs_block_machine_name_exists',
    ),
    '#required' => FALSE,
    '#default_value' => '',
  );
  $form['mefibs']['add_block']['submit'] = array(
    '#type' => 'submit',
    '#default_value' => t('Add block'),
    '#attributes' => array(
      'class' => array(
        'mefibs-add-block',
      ),
    ),
    // This is convenient to decide on the triggered action in
    // options_submit().
    '#parents' => array(
      'mefibs',
      'blocks',
      NULL,
      'add',
    ),
    '#group' => 'add',
    '#ajax' => array(
      'path' => current_path(),
    ),
  );
}