You are here

public function RssFields::buildOptionsForm in Views RSS 8.2

Same name in this branch
  1. 8.2 src/Plugin/views/style/RssFields.php \Drupal\views_rss\Plugin\views\style\RssFields::buildOptionsForm()
  2. 8.2 src/Plugin/views/row/RssFields.php \Drupal\views_rss\Plugin\views\row\RssFields::buildOptionsForm()
Same name and namespace in other branches
  1. 8.3 src/Plugin/views/style/RssFields.php \Drupal\views_rss\Plugin\views\style\RssFields::buildOptionsForm()

Provide a form to edit options for this plugin.

Overrides StylePluginBase::buildOptionsForm

File

src/Plugin/views/style/RssFields.php, line 123

Class

RssFields
Default style plugin to render an RSS feed from fields.

Namespace

Drupal\views_rss\Plugin\views\style

Code

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  parent::buildOptionsForm($form, $form_state);
  $initial_labels = array(
    '' => $this
      ->t('- None -'),
  );
  $view_fields_labels = $this->displayHandler
    ->getFieldLabels();
  $view_fields_labels = array_merge($initial_labels, $view_fields_labels);
  $form['uses_fields']['#type'] = 'hidden';

  // Element groups could be used both in channel and item settings.
  $element_groups = views_rss_get('element_groups');

  // Channel elements settings.
  $channel_elements = views_rss_get('channel_elements');
  if (count($channel_elements)) {
    foreach ($channel_elements as $module => $module_channel_elements) {
      foreach ($module_channel_elements as $element => $definition) {
        if (!isset($definition['configurable']) || $definition['configurable']) {
          list($namespace, $element_name) = views_rss_extract_element_names($element, 'core');

          // Add fieldset for namespace if not yet added.
          if (!isset($form['channel'][$namespace])) {
            $form['channel'][$namespace] = array(
              '#type' => 'details',
              '#title' => t('Channel elements : @namespace', array(
                '@namespace' => $namespace,
              )),
              '#description' => t('Provide values for &lt;channel&gt; elements in "@namespace" namespace. See <a href="@guide_url">Views RSS documentation</a> for more information.', array(
                '@namespace' => $namespace,
                '@guide_url' => Url::fromUri('http://drupal.org/node/1344136'),
              )),
              '#open' => FALSE,
            );
          }

          // Prepare form element.
          $default_value = NULL;
          if (!empty($this->options['channel'][$namespace][$module][$element_name])) {
            $default_value = $this->options['channel'][$namespace][$module][$element_name];
          }
          $form_item = array(
            '#type' => 'textfield',
            '#title' => Xss::filter(isset($definition['title']) ? $definition['title'] : $element_name),
            '#description' => Xss::filter(isset($definition['description']) ? $definition['description'] : NULL),
            '#default_value' => $default_value,
          );

          // Allow to overwrite default form element.
          if (!empty($definition['settings form'])) {
            $form_item = array_merge($form_item, $definition['settings form']);

            // Make sure that #options is an associative array.
            if (!empty($definition['settings form']['#options'])) {
              $form_item['#options'] = views_rss_map_assoc($definition['settings form']['#options']);
            }
          }
          if (!empty($definition['settings form options callback'])) {
            $function = $definition['settings form options callback'];
            $form_item['#options'] = views_rss_map_assoc($function());
          }

          // Add help link if provided.
          if (!empty($definition['help'])) {
            $form_item['#description'] .= ' ' . Link::fromTextAndUrl('[?]', Url::fromUri($definition['help']), array(
              'attributes' => array(
                'title' => t('Need more information?'),
              ),
            ))
              ->toString();
          }

          // Check if element should be displayed in a subgroup.
          if (!empty($definition['group'])) {

            // Add a subgroup to the form if it not yet added.
            if (!isset($form['channel'][$namespace][$module][$definition['group']])) {

              // Does module provide the group definition?
              $group_title = !empty($element_groups[$module][$definition['group']]['title']) ? $element_groups[$module][$definition['group']]['title'] : $definition['group'];
              $group_description = !empty($element_groups[$module][$definition['group']]['description']) ? $element_groups[$module][$definition['group']]['description'] : NULL;
              $form['channel'][$namespace][$module][$definition['group']] = array(
                '#type' => 'fieldset',
                '#title' => Xss::filter($group_title),
                '#description' => Xss::filter($group_description),
                '#collapsible' => TRUE,
                '#collapsed' => TRUE,
              );
            }
            $form['channel'][$namespace][$module][$definition['group']][$element_name] = $form_item;
          }
          else {
            $form['channel'][$namespace][$module][$element_name] = $form_item;
          }
        }
      }
    }
  }
  $form['namespaces'] = array(
    '#type' => 'details',
    '#title' => $this
      ->t('Namespaces'),
    '#open' => FALSE,
  );
  if (function_exists('rdf_get_namespaces')) {
    $form['namespaces']['add_rdf_namespaces'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Merge RDF namespaces'),
      '#description' => $this
        ->t('Enabling this option will merge RDF namespaces into the XML namespaces in case they are used in the RSS content.'),
      '#default_value' => $this->options['namespaces']['add_rdf_namespaces'],
    );
  }

  // Undefined namespaces derived from <channel> and/or <item>
  // elements defined by extension modules.
  $namespaces = views_rss_get('namespaces');
  if (count($namespaces)) {
    foreach ($namespaces as $module => $module_namespaces) {
      foreach ($module_namespaces as $namespace => $definition) {
        if (empty($definition['uri'])) {

          // Add fieldset for namespace if not yet added.
          if (!isset($form['namespaces'])) {
            $form['namespaces']['#description'] = t('Enter missing URLs for namespaces derived from &lt;channel&gt; and/or &lt;item&gt; elements defined by extension modules.');
          }
          if (!empty($this->options['namespaces'][$module][$namespace])) {
            $default_value = $this->options['namespaces'][$module][$namespace];
          }
          else {
            $default_value = NULL;
          }
          $form['namespaces'][$module][$namespace] = array(
            '#type' => 'textfield',
            '#title' => check_plain($namespace),
            '#default_value' => $default_value,
          );
        }
      }
    }
  }

  // Other feed settings.
  $form['feed_settings'] = array(
    '#type' => 'details',
    '#title' => $this
      ->t('Other feed settings'),
    '#open' => FALSE,
  );
  $form['feed_settings']['absolute_paths'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t("Replace relative paths with absolute URLs"),
    '#description' => $this
      ->t('Enabling this option will replace all relative paths (like <em>/node/1</em>) with absolute URLs (<em>!absolute_url</em>) in all feed elements configured to use this feature (for example &lt;description&gt; element).', array(
      '!absolute_url' => trim($GLOBALS['base_url'], '/') . '/node/1',
    )),
    '#default_value' => !empty($this->options['feed_settings']['absolute_paths']),
    '#weight' => 1,
  );
  $form['feed_settings']['feed_in_links'] = array(
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Display feed icon in the links attached to the view'),
    '#default_value' => !empty($this->options['feed_settings']['feed_in_links']),
    '#weight' => 3,
  );
}