You are here

function feedapi_form_alter in FeedAPI 6

Same name and namespace in other branches
  1. 5 feedapi.module \feedapi_form_alter()

Implementation of hook_form_alter().

File

./feedapi.module, line 352
Handle the submodules (for feed and item processing) Provide a basic management of feeds

Code

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

  // Content type form.
  if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
    $node_type_settings = feedapi_get_settings($form['#node_type']->type);
    $form['#validate'][] = 'feedapi_content_type_validate';

    // Don't blow away existing form elements.
    if (!isset($form['feedapi'])) {
      $form['feedapi'] = array();
    }
    $form['feedapi'] += array(
      '#type' => 'fieldset',
      '#title' => t('Feed API'),
      '#collapsible' => TRUE,
      '#collapsed' => isset($node_type_settings['enabled']) ? !$node_type_settings['enabled'] : TRUE,
      '#tree' => TRUE,
    );
    $form['feedapi']['enabled'] = array(
      '#type' => 'checkbox',
      '#title' => t('Is a feed content type'),
      '#description' => t('Check if you want to use this content type for downloading feeds to your site.'),
      '#default_value' => isset($node_type_settings['enabled']) ? $node_type_settings['enabled'] : FALSE,
      '#weight' => -15,
    );
    $form['feedapi']['upload_method'] = array(
      '#type' => 'radios',
      '#title' => t('Supply feed as'),
      '#description' => t('Select how a user will supply a feed. Choose URL if the user will paste a URL to a textfield, choose File upload if the user will upload a feed from the local disk.'),
      '#options' => array(
        'url' => t('URL'),
        'upload' => t('File upload'),
      ),
      '#default_value' => isset($node_type_settings['upload_method']) ? $node_type_settings['upload_method'] : 'url',
      '#weight' => -14,
    );
    $modules = module_implements('feedapi_settings_form');
    foreach ($modules as $module) {
      $form['feedapi']['defaults'] = array(
        '#type' => 'markup',
        '#value' => '<strong>' . t('Default settings') . '</strong><hr/>',
      );
      if ($feedapi_form = module_invoke($module, 'feedapi_settings_form', 'general')) {
        $form['feedapi'] = array_merge_recursive($form['feedapi'], $feedapi_form);
      }
    }
    $form['feedapi']['parsers'] = array(
      '#type' => 'fieldset',
      '#title' => t('Parser settings'),
      '#description' => t('Parsers turn a feed into an object ready for processing. Choose at least one.'),
      '#collapsible' => FALSE,
      '#tree' => TRUE,
    );
    $parsers = module_implements('feedapi_feed', TRUE);
    rsort($parsers);
    foreach ($parsers as $parser) {
      $form['feedapi']['parsers'][$parser] = array(
        '#type' => 'fieldset',
        '#title' => feedapi_get_natural_name($parser),
        '#collapsible' => TRUE,
        '#collapsed' => isset($node_type_settings['parsers'][$parser]['enabled']) ? !$node_type_settings['parsers'][$parser]['enabled'] : TRUE,
        '#tree' => TRUE,
        '#weight' => isset($node_type_settings['parsers'][$parser]['weight']) ? $node_type_settings['parsers'][$parser]['weight'] : 0,
      );
      $form['feedapi']['parsers'][$parser]['enabled'] = array(
        '#type' => 'checkbox',
        '#title' => t('Enable'),
        '#description' => t('Check this box if you want to enable the @name parser on this feed.', array(
          '@name' => $parser,
        )),
        '#default_value' => isset($node_type_settings['parsers'][$parser]['enabled']) ? $node_type_settings['parsers'][$parser]['enabled'] : FALSE,
        '#weight' => -15,
      );
      $form['feedapi']['parsers'][$parser]['weight'] = array(
        '#type' => 'weight',
        '#delta' => 15,
        '#title' => t('Weight'),
        '#description' => t('Control the execution order. Parsers with lower weights are called before parsers with higher weights.'),
        '#default_value' => isset($node_type_settings['parsers'][$parser]['weight']) ? $node_type_settings['parsers'][$parser]['weight'] : 0,
        '#weight' => -14,
      );
      if ($parser_form = module_invoke($parser, 'feedapi_settings_form', 'parsers')) {
        $form['feedapi']['parsers'][$parser]['defaults'] = array(
          '#type' => 'markup',
          '#value' => '<strong>' . t('Default settings') . '</strong><hr/>',
        );
        $form['feedapi']['parsers'][$parser] = array_merge_recursive($form['feedapi']['parsers'][$parser], $parser_form);
      }
    }
    $form['feedapi']['processors'] = array(
      '#type' => 'fieldset',
      '#title' => t('Processor settings'),
      '#description' => t('Processors are any kind of add on modules that hook into the feed handling process on download time - you can decide here what should happen to feed items once they are downloaded and parsed.'),
      '#collapsible' => FALSE,
      '#tree' => TRUE,
    );
    $processors = module_implements('feedapi_item', TRUE);
    rsort($processors);
    foreach ($processors as $processor) {
      $form['feedapi']['processors'][$processor] = array(
        '#type' => 'fieldset',
        '#title' => feedapi_get_natural_name($processor),
        '#collapsible' => TRUE,
        '#collapsed' => isset($node_type_settings['processors'][$processor]['enabled']) ? !$node_type_settings['processors'][$processor]['enabled'] : TRUE,
        '#tree' => TRUE,
        '#weight' => isset($node_type_settings['processors'][$processor]['weight']) ? $node_type_settings['processors'][$processor]['weight'] : 0,
      );
      $form['feedapi']['processors'][$processor]['enabled'] = array(
        '#type' => 'checkbox',
        '#title' => t('Enable'),
        '#description' => t('Check this box if you want to enable the @name processor on this feed.', array(
          '@name' => $processor,
        )),
        '#default_value' => isset($node_type_settings['processors'][$processor]['enabled']) ? $node_type_settings['processors'][$processor]['enabled'] : FALSE,
        '#weight' => -15,
      );
      $form['feedapi']['processors'][$processor]['weight'] = array(
        '#type' => 'weight',
        '#delta' => 15,
        '#title' => t('Weight'),
        '#description' => t('Control the execution order. Processors with lower weights are called before processors with higher weights.'),
        '#default_value' => isset($node_type_settings['processors'][$processor]['weight']) ? $node_type_settings['processors'][$processor]['weight'] : 0,
        '#weight' => -14,
      );
      if ($processor_form = module_invoke($processor, 'feedapi_settings_form', 'processors')) {
        $form['feedapi']['processors'][$processor]['defaults'] = array(
          '#type' => 'markup',
          '#value' => '<strong>' . t('Default settings') . '</strong><hr/>',
        );
        $form['feedapi']['processors'][$processor] = array_merge_recursive($form['feedapi']['processors'][$processor], $processor_form);
      }
    }

    // Populate form with node type settings if available.
    if ($node_type_settings) {
      $form['feedapi'] = _feedapi_populate($form['feedapi'], $node_type_settings);
    }
    $form['#submit'][] = 'feedapi_content_type_submit';
  }
  elseif (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] . '_node_form' == $form_id && feedapi_enabled_type($form['type']['#value'])) {

    // Get settings for corresponding content type
    // Which parsers / processors are enabled is a per content-type setting.
    $node_type_settings = feedapi_get_settings($form['type']['#value']);

    // FeedAPI-enabled node form.
    $form['title']['#required'] = FALSE;
    $form['title']['#description'] = t('This field will be populated with the feed title. You can override by filling in this field.');
    $form['body_field']['body']['#description'] = t('This field will be populated with the feed description. You can override by filling in this field.');
    $form['body_field']['body']['#rows'] = 2;

    // Don't blow away existing form elements.
    if (!isset($form['feedapi'])) {
      $form['feedapi'] = array();
    }
    $form['feedapi'] += array(
      '#type' => 'fieldset',
      '#title' => t('Feed'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#tree' => TRUE,
    );
    $feedapi_url_default = '';
    if (isset($form['#node']->feed->url)) {
      $feedapi_url_default = $form['#node']->feed->url;
    }
    elseif (isset($form_state['values']['feedapi']['feedapi_url'])) {
      $feedapi_url_default = $form_state['values']['feedapi']['feedapi_url'];
    }
    if (isset($node_type_settings['upload_method']) && $node_type_settings['upload_method'] == 'upload') {

      // Makes possible to upload file via this form.
      $form['#attributes']['enctype'] = 'multipart/form-data';
      $form['feedapi']['feedapi_file'] = array(
        '#type' => 'file',
        '#title' => t('Upload a feed'),
        '#description' => $feedapi_url_default ? '<div class="feed-url">' . $feedapi_url_default . '</div>' : '',
        '#size' => 40,
      );
      $form['feedapi']['feedapi_url'] = array(
        '#type' => 'value',
        '#value' => $feedapi_url_default,
      );
    }
    else {
      $form['feedapi']['feedapi_url'] = array(
        '#type' => 'textfield',
        '#title' => t('Feed URL'),
        '#description' => t('Enter feed URL. The set of supported schemas (e.g. ftp://, http://) depends on the parser that you use.'),
        '#default_value' => $feedapi_url_default,
        '#maxlength' => 2048,
      );
    }

    // Show per-node-type feedapi, parser options only for users with permissions.
    if (user_access('advanced feedapi options')) {

      // retrieve forms.
      $modules = module_implements('feedapi_settings_form');
      foreach ($modules as $module) {
        if ($feedapi_form = module_invoke($module, 'feedapi_settings_form', 'general')) {
          $form['feedapi'] = array_merge_recursive($form['feedapi'], $feedapi_form);
        }
      }
      $submodules_names = array(
        'parsers' => t('Parsers'),
        'processors' => t('Processors'),
      );
      foreach (array(
        "parsers" => "feedapi_feed",
        "processors" => "feedapi_item",
      ) as $type => $requirement) {
        $suitable_handlers = module_implements($requirement, TRUE);
        foreach ($suitable_handlers as $module) {
          if (isset($node_type_settings[$type][$module]) && $node_type_settings[$type][$module]['enabled']) {
            $result = array();
            $result = module_invoke($module, 'feedapi_settings_form', $type);
            if (is_array($result)) {
              $result['#weight'] = $node_type_settings[$type][$module]['weight'];
              $form['feedapi'][$type][$module] = $result;
              $form['feedapi'][$type][$module]['#type'] = 'fieldset';
              $form['feedapi'][$type][$module]['#title'] = feedapi_get_natural_name($module);
              $form['feedapi'][$type][$module]['#collapsible'] = TRUE;
              $form['feedapi'][$type][$module]['#collapsed'] = FALSE;
              $form['feedapi'][$type][$module]['#tree'] = TRUE;
            }
          }
        }
        if (isset($form['feedapi'][$type])) {
          $form['feedapi'][$type]['#type'] = 'fieldset';
          $form['feedapi'][$type]['#title'] = $submodules_names[$type];
          $form['feedapi'][$type]['#collapsible'] = TRUE;
          $form['feedapi'][$type]['#collapsed'] = TRUE;
          $form['feedapi'][$type]['#tree'] = TRUE;
        }
      }
    }

    // If we are on a node form, get per node settings and populate form.
    if (isset($form['#node']->nid)) {
      $settings = feedapi_get_settings($form['type']['#value'], $form['#node']->vid);
    }
    elseif (isset($node_type_settings)) {
      $settings = $node_type_settings;
    }
    if (isset($settings)) {
      $form['feedapi'] = _feedapi_populate($form['feedapi'], $settings);
    }
    $form['#validate'][] = 'feedapi_node_validate';
  }
}