FeedFormController.php 5.64 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Contains \Drupal\aggregator\FeedFormController.
 */

namespace Drupal\aggregator;

10
use Drupal\Component\Utility\String;
11
use Drupal\Core\Entity\ContentEntityFormController;
12
use Drupal\Core\Entity\EntityManagerInterface;
13
use Drupal\Core\Language\Language;
14
15
use Drupal\aggregator\CategoryStorageControllerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
16
17
18
19

/**
 * Form controller for the aggregator feed edit forms.
 */
20
class FeedFormController extends ContentEntityFormController {
21

22
23
24
25
26
27
28
29
30
31
  /**
   * The category storage controller.
   *
   * @var \Drupal\aggregator\CategoryStorageControllerInterface
   */
  protected $categoryStorageController;

  /**
   * Constructs a FeedForm object.
   *
32
33
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
34
35
36
   * @param \Drupal\aggregator\CategoryStorageControllerInterface $category_storage_controller
   *   The category storage controller.
   */
37
38
  public function __construct(EntityManagerInterface $entity_manager, CategoryStorageControllerInterface $category_storage_controller) {
    parent::__construct($entity_manager);
39
40
41
42
43
44
45
46
    $this->categoryStorageController = $category_storage_controller;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
47
      $container->get('entity.manager'),
48
49
50
51
      $container->get('aggregator.category.storage')
    );
  }

52
  /**
53
   * {@inheritdoc}
54
   */
55
56
  public function form(array $form, array &$form_state) {
    $feed = $this->entity;
57
    $period = drupal_map_assoc(array(900, 1800, 3600, 7200, 10800, 21600, 32400, 43200, 64800, 86400, 172800, 259200, 604800, 1209600, 2419200), 'format_interval');
58
    $period[AGGREGATOR_CLEAR_NEVER] = $this->t('Never');
59
60
61

    $form['title'] = array(
      '#type' => 'textfield',
62
      '#title' => $this->t('Title'),
63
64
      '#default_value' => $feed->label(),
      '#maxlength' => 255,
65
      '#description' => $this->t('The name of the feed (or the name of the website providing the feed).'),
66
67
      '#required' => TRUE,
    );
68
69

    $form['langcode'] = array(
70
      '#title' => $this->t('Language'),
71
      '#type' => 'language_select',
72
      '#default_value' => $feed->language()->id,
73
      '#languages' => Language::STATE_ALL,
74
75
    );

76
77
    $form['url'] = array(
      '#type' => 'url',
78
      '#title' => $this->t('URL'),
79
80
      '#default_value' => $feed->url->value,
      '#maxlength' => NULL,
81
      '#description' => $this->t('The fully-qualified URL of the feed.'),
82
83
84
      '#required' => TRUE,
    );
    $form['refresh'] = array('#type' => 'select',
85
      '#title' => $this->t('Update interval'),
86
87
      '#default_value' => $feed->refresh->value,
      '#options' => $period,
88
      '#description' => $this->t('The length of time between feed updates. Requires a correctly configured <a href="@cron">cron maintenance task</a>.', array('@cron' => url('admin/reports/status'))),
89
90
91
92
93
    );

    // Handling of categories.
    $options = array();
    $values = array();
94
95
96
97
98
    $categories = $this->categoryStorageController->loadAllKeyed();
    foreach ($categories as $cid => $title) {
      $options[$cid] = String::checkPlain($title);
      if (!empty($feed->categories) && in_array($cid, array_keys($feed->categories))) {
        $values[] = $cid;
99
100
101
102
103
104
      }
    }

    if ($options) {
      $form['category'] = array(
        '#type' => 'checkboxes',
105
        '#title' => $this->t('Categorize news items'),
106
107
        '#default_value' => $values,
        '#options' => $options,
108
        '#description' => $this->t('New feed items are automatically filed in the checked categories.'),
109
110
111
112
113
114
115
      );
    }

    return parent::form($form, $form_state, $feed);
  }

  /**
116
   * {@inheritdoc}
117
118
119
120
   */
  public function validate(array $form, array &$form_state) {
    $feed = $this->buildEntity($form, $form_state);
    // Check for duplicate titles.
121
122
    $feed_storage_controller = $this->entityManager->getStorageController('aggregator_feed');
    $result = $feed_storage_controller->getFeedDuplicates($feed);
123
124
    foreach ($result as $item) {
      if (strcasecmp($item->title, $feed->label()) == 0) {
125
        form_set_error('title', $this->t('A feed named %feed already exists. Enter a unique title.', array('%feed' => $feed->label())));
126
127
      }
      if (strcasecmp($item->url, $feed->url->value) == 0) {
128
        form_set_error('url', $this->t('A feed with this URL %url already exists. Enter a unique URL.', array('%url' => $feed->url->value)));
129
130
131
132
133
134
      }
    }
    parent::validate($form, $form_state);
  }

  /**
135
   * {@inheritdoc}
136
137
   */
  public function save(array $form, array &$form_state) {
138
    $feed = $this->entity;
139
140
141
    $insert = (bool) $feed->id();
    if (!empty($form_state['values']['category'])) {
      // Store category values for post save operations.
142
      // @see \Drupal\Core\Entity\FeedStorageController::postSave()
143
144
145
146
      $feed->categories = $form_state['values']['category'];
    }
    $feed->save();
    if ($insert) {
147
      drupal_set_message($this->t('The feed %feed has been updated.', array('%feed' => $feed->label())));
148
149
150
151
152
153
154
155
      if (arg(0) == 'admin') {
        $form_state['redirect'] = 'admin/config/services/aggregator';
      }
      else {
        $form_state['redirect'] = 'aggregator/sources/' . $feed->id();
      }
    }
    else {
156
157
      watchdog('aggregator', 'Feed %feed added.', array('%feed' => $feed->label()), WATCHDOG_NOTICE, l($this->t('view'), 'admin/config/services/aggregator'));
      drupal_set_message($this->t('The feed %feed has been added.', array('%feed' => $feed->label())));
158
159
160
161
    }
  }

  /**
162
   * {@inheritdoc}
163
164
   */
  public function delete(array $form, array &$form_state) {
165
    $form_state['redirect'] = 'admin/config/services/aggregator/delete/feed/' . $this->entity->id();
166
167
168
  }

}