You are here

function panopoly_magic_form_views_content_views_panes_content_type_edit_form_alter in Panopoly Magic 7

Implementation of hook_form_FORM_ID_alter()

Provides customizations to the views content type settings form

File

./panopoly_magic.module, line 928

Code

function panopoly_magic_form_views_content_views_panes_content_type_edit_form_alter(&$form, &$form_state, $form_id) {

  // Get the configuration
  $conf = $form_state['conf'];

  // Add a widget title setting if views allows this to be customized
  if (!empty($form['override_title'])) {
    $form['override_title_markup']['#access'] = FALSE;
    $form['override_title']['#access'] = FALSE;
    $form['override_title_text']['#access'] = FALSE;
    $form['override_title_heading']['#access'] = FALSE;
    $form['exposed']['widget_title'] = array(
      '#field_prefix' => t('Title'),
      '#type' => 'textfield',
      '#size' => '',
      '#weight' => -100,
      '#states' => array(
        'visible' => array(
          ':input[name="override_title"]' => array(
            'value' => '1',
          ),
        ),
      ),
      '#default_value' => isset($conf['widget_title']) ? $conf['widget_title'] : $form_state['view']->display_handler->options['title'],
      '#parents' => array(
        'widget_title',
      ),
    );
  }

  // Add an option to make the widget title a link if views allows this to happen
  if (!empty($form['link_to_view']) && !empty($form['path'])) {
    $form['link_to_view']['#title'] = t('Make title a link');
    $form['link_to_view']['#description'] = '';
    $form['link_to_view']['#id'] = 'edit-link';
    $form['exposed']['link_to_view'] = $form['link_to_view'];
    $form['exposed']['link_to_view']['#parents'] = array(
      'link_to_view',
    );
    unset($form['link_to_view']);
  }

  // Add an option to make the more link available if views allows this to happen
  if (!empty($form['more_link'])) {
    $form['more_link']['#id'] = 'more-link';
    $form['more_link']['#description'] = '';
    $form['exposed']['more_link'] = $form['more_link'];
    $form['exposed']['more_link']['#parents'] = array(
      'more_link',
    );
    unset($form['more_link']);
  }

  // Add an option to the more link text if views allows this to happen
  if (!empty($form['more_text'])) {
    $form['exposed']['more_text'] = $form['more_text'];
    $form['exposed']['more_text']['#parents'] = array(
      'more_text',
    );
    unset($form['more_text']);
    $form['exposed']['more_text']['#states'] = array(
      'visible' => array(
        ':input[name="more_link"]' => array(
          'checked' => TRUE,
        ),
      ),
    );
  }

  // Update the field settings for pagers
  if (!empty($form['use_pager']) && !empty($form['pager_id'])) {
    $form['use_pager']['#prefix'] = '<div class="form-item container-inline">';
    $form['exposed']['use_pager'] = $form['use_pager'];
    $form['exposed']['use_pager']['#parents'] = array(
      'use_pager',
    );
    $form['exposed']['pager_id'] = $form['pager_id'];
    $form['exposed']['pager_id']['#parents'] = array(
      'pager_id',
    );
    unset($form['use_pager']);
    unset($form['pager_id']);
  }

  // Handle the path value that views gives us
  if (!empty($form['path'])) {
    $form['path']['#description'] = t('The URL path used for linking and paging purposes. Leave blank to use the current page.');
    $form['path']['#title'] = '';
    $form['path']['#dependency'] = array(
      'edit-link' => array(
        1,
      ),
      'more-link' => array(
        1,
      ),
      'use-pager-checkbox' => array(
        1,
      ),
    );
    $form['exposed']['path'] = $form['path'];
    $form['exposed']['path']['#parents'] = array(
      'path',
    );
    unset($form['path']);
  }

  // Adjust the items per page and offset settings
  if (!empty($form['items_per_page']) && !empty($form['offset'])) {
    $form['items_per_page']['#field_prefix'] = t('Items to Show');
    $form['items_per_page']['#title'] = '';
    $form['items_per_page']['#description'] = t('Enter 0 to display all.');
    $form['display_settings']['items_per_page'] = $form['items_per_page'];
    unset($form['items_per_page']);
    $form['offset']['#field_prefix'] = t('Items to Skip');
    $form['offset']['#title'] = '';
    $form['offset']['#description'] = t('Enter 0 to skip none.');
    $form['offset']['#prefix'] = '<div class="clearfix">';
    $form['offset']['#suffix'] = '</div>';
    $form['display_settings']['offset'] = $form['offset'];
    unset($form['offset']);
  }
  elseif (!empty($form['items_per_page']) && empty($form['offset'])) {
    $form['items_per_page']['#field_prefix'] = t('Items to Show');
    $form['items_per_page']['#title'] = '';
    $form['items_per_page']['#description'] = t('Enter 0 to display all.');
    $form['items_per_page']['#prefix'] = '<div class="clearfix">';
    $form['items_per_page']['#suffix'] = '</div>';
    $form['display_settings']['items_per_page'] = $form['items_per_page'];
    unset($form['items_per_page']);
  }
  elseif (empty($form['items_per_page']) && !empty($form['offset'])) {
    $form['offset']['#field_prefix'] = t('Items to Skip');
    $form['offset']['#title'] = '';
    $form['offset']['#description'] = t('Enter 0 to skip none.');
    $form['offset']['#prefix'] = '<div class="clearfix">';
    $form['offset']['#suffix'] = '</div>';
    $form['display_settings']['offset'] = $form['offset'];
    unset($form['offset']);
  }

  // Adjust the sort ordering and sort by options
  if (!empty($form['exposed']['sort_order']) && !empty($form['exposed']['sort_by'])) {
    $form['exposed']['sort_order']['#title'] = t('Sort order');
    $form['display_settings']['sort_order'] = $form['exposed']['sort_order'];
    $form['display_settings']['sort_order']['#parents'] = array(
      'exposed',
      'sort_order',
    );
    unset($form['exposed']['sort_order']);
    $form['exposed']['sort_by']['#prefix'] = '<div class="clearfix">';
    $form['exposed']['sort_by']['#suffix'] = '</div>';
    $form['display_settings']['sort_by'] = $form['exposed']['sort_by'];
    $form['display_settings']['sort_by']['#parents'] = array(
      'exposed',
      'sort_by',
    );
    unset($form['exposed']['sort_by']);
  }
  elseif (!empty($form['exposed']['sort_order']) && empty($form['exposed']['sort_by'])) {
    $form['exposed']['sort_order']['#title'] = t('Sort order');
    $form['exposed']['#prefix'] = '<div class="clearfix">';
    $form['exposed']['#suffix'] = '</div>';
    $form['display_settings']['sort_order'] = $form['exposed']['sort_order'];
    $form['display_settings']['sort_order']['#parents'] = array(
      'exposed',
      'sort_order',
    );
    unset($form['exposed']['sort_order']);
  }
  elseif (empty($form['exposed']['sort_order']) && !empty($form['exposed']['sort_by'])) {
    $form['exposed']['sort_by']['#prefix'] = '<div class="clearfix">';
    $form['exposed']['sort_by']['#suffix'] = '</div>';
    $form['display_settings']['sort_by'] = $form['exposed']['sort_by'];
    $form['display_settings']['sort_by']['#parents'] = array(
      'exposed',
      'sort_by',
    );
    unset($form['exposed']['sort_by']);
  }

  // Adjust the field setting options
  if (isset($form['fields_override'])) {
    $form['fields_override']['#title'] = t('Field Settings');
    $form['fields_override']['#collapsible'] = FALSE;
    foreach ($form['fields_override'] as &$field) {
      if (is_array($field)) {
        $field['#title'] = t('Display') . ' ' . $field['#title'];
      }
    }
  }

  // Determine if this is a fielded view. If so, add magic display type changer
  $view_handler = $form_state['view']->display_handler;
  if ($view_handler
    ->get_option('row_plugin') == 'fields') {

    // Set to default view settings if there isn't one.
    if (empty($conf['view_settings'])) {
      if ($form_state['view']->style_plugin->plugin_name == 'table') {
        $conf['view_settings'] = 'table';
      }
      else {
        $conf['view_settings'] = 'fields';
      }
    }

    // Deal with legacy 'nodes' and others (such as 'files') view settings so
    // that other entity types can be included.
    $conf['view_settings'] = panopoly_magic_convert_view_settings($conf['view_settings']);

    // Add information about the View Mode
    $form['display_settings']['view_settings'] = array(
      '#type' => 'radios',
      '#prefix' => '<div class="view-settings-wrapper">',
      '#suffix' => '</div>',
      '#title' => t('Display Type'),
      '#default_value' => $conf['view_settings'],
      '#weight' => 10,
      '#options' => array(
        'fields' => t('Fields'),
        'rendered_entity' => t('Content'),
        'table' => t('Table'),
      ),
    );

    // Add header column options for table views.
    $form['display_settings']['header_type'] = array(
      '#type' => 'select',
      '#title' => t('Column Header'),
      '#options' => array(
        'none' => t('None'),
        'titles' => t('Titles'),
      ),
      '#default_value' => !empty($conf['header_type']) ? $conf['header_type'] : 'none',
      '#states' => array(
        'visible' => array(
          ':input[name="view_settings"]' => array(
            'value' => 'table',
          ),
        ),
      ),
      '#weight' => 11,
    );

    // Update the field overrides to be dependent on the view settings selection.
    $form['fields_override']['#states'] = array(
      // The inverted logic here isn't optimal, and in the future may be better achieved via OR'd conditions.
      // @link http://drupal.org/node/735528 @endlink
      'invisible' => array(
        ':input[name="view_settings"]' => array(
          'value' => 'rendered_entity',
        ),
      ),
    );

    // If the View has opted out of 'Display type' then we hide it from the form,
    // but still allow our JavaScript #states to work. We do the is_a() check to
    // not break things in the case the user has upgraded panopoly_magic, but
    // hasn't yet rebuilt the Views cache.
    $view_allow = $view_handler
      ->get_option('allow');
    if (is_a($view_handler, 'panopoly_magic_plugin_display_panel_pane') && empty($view_allow['panopoly_magic_display_type'])) {
      $form['display_settings']['view_settings']['#type'] = 'hidden';
      $form['display_settings']['header_type']['#type'] = 'hidden';

      // Since we're not giving the user the choice, default to keeping the titles.
      $form['display_settings']['header_type']['#default_value'] = 'titles';
    }
  }

  // Get view modes for entity
  $view_modes = panopoly_magic_view_mode_options(panopoly_magic_get_entity_type($form_state['view']));

  // Get the default view mode.
  $options_default_view_mode = $view_handler
    ->get_option('row_plugin') == 'fields' ? 'teaser' : 'full';
  $row_options = $view_handler
    ->get_option('row_options');
  if (isset($form_state['view']->style_plugin->row_plugin->options['view_mode'])) {
    $options_default_view_mode = $form_state['view']->style_plugin->row_plugin->options['view_mode'];
  }
  if (!array_key_exists($options_default_view_mode, $view_modes)) {
    $options_default_view_mode = current(array_keys($view_modes));
  }

  // Add specific style options.
  $form['content_settings']['view_mode'] = array(
    '#type' => 'radios',
    '#options' => $view_modes,
    '#default_value' => !empty($conf['view_mode']) ? $conf['view_mode'] : $options_default_view_mode,
    '#states' => array(
      'visible' => array(
        ':input[name="view_settings"]' => array(
          'value' => 'rendered_entity',
        ),
      ),
    ),
  );

  // Show view mode selection if the display type setting exists because the
  // view mode will get shown/hidden by Javascript.
  if (isset($form['display_settings']['view_settings'])) {
    $form['content_settings']['view_mode']['#access'] = TRUE;
  }
  elseif (!in_array($view_handler
    ->get_option('row_plugin'), array(
    'node',
    'entity',
  )) || $form_state['view']->style_plugin->plugin_name == 'table') {
    $form['content_settings']['view_mode']['#access'] = FALSE;
  }

  // If the View has opted out of 'View mode' then we hide it from the form.
  // We do the is_a() check to not break things in the case the user has
  // upgraded panopoly_magic, but hasn't yet rebuilt the Views cache.
  $view_allow = $view_handler
    ->get_option('allow');
  if (is_a($view_handler, 'panopoly_magic_plugin_display_panel_pane') && empty($view_allow['panopoly_magic_view_mode'])) {
    $form['content_settings']['view_mode']['#access'] = FALSE;
  }

  // Define a general settings fieldset if we have exposed values
  if (!empty($form['exposed']) && count(_panopoly_magic_visible_element_children($form['exposed'])) > 0) {
    $form['exposed']['#type'] = 'fieldset';
    $form['exposed']['#title'] = t('General Settings');
    $form['exposed']['#weight'] = -30;
    $form['exposed']['#attributes'] = array(
      'class' => array(
        'general-settings-fieldset',
      ),
    );
  }

  // Define a context settings fieldset if we have exposed values
  if (!empty($form['context']) && count(_panopoly_magic_visible_element_children($form['context'])) > 0) {
    $form['context']['#type'] = 'fieldset';
    $form['context']['#title'] = t('Context Settings');
    $form['context']['#weight'] = -29;
  }

  // Define a content settings fieldset if we have exposed values
  if (!empty($form['content_settings']) && count(_panopoly_magic_visible_element_children($form['content_settings'])) > 0) {
    $form['content_settings']['#type'] = 'fieldset';
    $form['content_settings']['#title'] = t('Content Settings');
    $form['content_settings']['#weight'] = -27;
  }

  // Define a display settings fieldset if we have display options
  if (!empty($form['display_settings']) && count(_panopoly_magic_visible_element_children($form['display_settings'])) > 0) {
    $form['display_settings']['#type'] = 'fieldset';
    $form['display_settings']['#title'] = t('Display Settings');
    $form['display_settings']['#weight'] = -28;
  }

  // Add auto submit functionality
  if (variable_get('panopoly_magic_live_preview', 1)) {
    panopoly_magic_autosubmit_configure($form);
  }

  // Add a custom submit handler to our preview and submit option
  $form['#submit'][] = 'panopoly_magic_views_content_type_modal_submit';
}