You are here

function feedapi_form_alter in FeedAPI 5

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

Implementation of hook_form_alter().

File

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

Code

function feedapi_form_alter($form_id, &$form) {

  // Content type form.
  if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
    if (isset($form['#post']['feedapi'])) {

      // TODO: Drupal automatically stores mutilated 'feedapi_'. $form['#node_type']->type - remove.
      $type = !empty($form['#node_type']->type) ? $form['#node_type']->type : $form['#post']['type'];
      _feedapi_store_settings(array(
        'node_type' => $type,
      ), $form['#post']['feedapi']);
    }
    $node_type_settings = feedapi_get_settings($form['#node_type']->type);
    $form['feedapi'] = array(
      '#type' => 'fieldset',
      '#title' => t('Feed API'),
      '#collapsible' => TRUE,
      '#collapsed' => isset($node_type_settings['enabled']) ? FALSE : TRUE,
      '#tree' => TRUE,
      '#validate' => array(
        'feedapi_content_type_validate' => array(),
      ),
    );
    $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,
    );
    $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' => !($node_type_settings['parsers'][$parser]['enabled'] || count($parsers) == 1),
        '#tree' => TRUE,
        '#weight' => $node_type_settings['parsers'][$parser]['weight'],
      );
      $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' => t($parser),
        )),
        '#default_value' => $node_type_settings['parsers'][$parser]['enabled'] || count($parsers) == 1,
        '#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' => $node_type_settings['parsers'][$parser]['weight'],
        '#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' => !($node_type_settings['processors'][$processor]['enabled'] || count($processors) == 1),
        '#tree' => TRUE,
        '#weight' => $node_type_settings['processors'][$processor]['weight'],
      );
      $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' => t($processor),
        )),
        '#default_value' => $node_type_settings['processors'][$processor]['enabled'],
        '#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' => $node_type_settings['processors'][$processor]['weight'],
        '#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);
    }

    // We add again to ensure the correct order. Without it, the submit and delete buttons are in a wrong place (not last)
    $submit = $form['submit'];
    $delete = $form['delete'];
    unset($form['submit'], $form['delete']);
    $form['submit'] = $submit;
    $form['delete'] = $delete;
  }

  // FeedAPI-enabled node form.
  if (isset($form['type']) && $form['type']['#value'] . '_node_form' == $form_id && feedapi_enabled_type($form['type']['#value'])) {
    $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_filter']['body']['#description'] = t('This field will be populated with the feed description. You can override by filling in this field.');
    $form['feedapi'] = array(
      '#type' => 'fieldset',
      '#title' => t('Feed'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#tree' => TRUE,
    );
    if ($_GET['feedapi_url']) {
      $feedapi_url_default = $_GET['feedapi_url'];
    }
    else {
      if ($form['#node']->feed->url) {
        $feedapi_url_default = $form['#node']->feed->url;
      }
      else {
        if ($form['#post']['feedapi']['feedapi_url']) {
          $feedapi_url_default = $form['#post']['feedapi']['feedapi_url'];
        }
      }
    }
    $form['feedapi']['feedapi_url'] = array(
      '#type' => 'textfield',
      '#title' => t('Feed URL'),
      '#description' => t('Enter feed URL.'),
      '#default_value' => $feedapi_url_default,
      '#maxlength' => 2048,
    );

    // Build feed object on validate and submit.
    if ($form['#post']['op']) {
      $feed = _feedapi_build_feed_object($form['type']['#value'], $form['#post']['feedapi']['feedapi_url']);

      // Stick feed object into feedapi form snippet - store it in submit.
      $form['feedapi_feed_object'] = array(
        '#type' => 'value',
        '#value' => $feed,
      );
      if (!$form['#post']['title']) {
        $form['title']['#value'] = $feed->title;
      }
      if (!$form['#post']['body']) {
        $form['body_filter']['body']['#value'] = $feed->description;
      }
    }

    // Show per-node-type feedapi, parser options only for users with permissions.
    if (user_access('advanced feedapi options')) {
      $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);
        }
      }

      // 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']);

      // retrieve forms.
      foreach (array(
        "parsers" => "feedapi_feed",
        "processors" => "feedapi_item",
      ) as $type => $requirement) {
        $suitable_handlers = module_implements($requirement, TRUE);
        foreach ($suitable_handlers as $module) {
          if ($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'] = t(ucfirst($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 (!($settings = feedapi_get_settings($form['type']['#value'], $form['#node']->nid))) {
      $settings = $node_type_settings;
    }
    $form['feedapi'] = _feedapi_populate($form['feedapi'], $settings);
  }
}