You are here

class FeedTypeForm in Feeds 8.3

Form controller for the feed type edit forms.

Hierarchy

Expanded class hierarchy of FeedTypeForm

File

src/FeedTypeForm.php, line 26

Namespace

Drupal\feeds
View source
class FeedTypeForm extends EntityForm {

  /**
   * The feed type storage controller.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $feedTypeStorage;

  /**
   * The form factory.
   *
   * @var \Drupal\feeds\Plugin\PluginFormFactory
   */
  protected $formFactory;

  /**
   * Provides a service to handle various date related functionality.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * Turns a render array into a HTML string.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Constructs a new FeedTypeForm object.
   *
   * @param \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $feed_type_storage
   *   The feed type storage controller.
   * @param \Drupal\feeds\Plugin\PluginFormFactory $factory
   *   The form factory.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The services of date.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The render object.
   */
  public function __construct(ConfigEntityStorageInterface $feed_type_storage, PluginFormFactory $factory, DateFormatterInterface $date_formatter, RendererInterface $renderer) {
    $this->feedTypeStorage = $feed_type_storage;
    $this->formFactory = $factory;
    $this->dateFormatter = $date_formatter;
    $this->renderer = $renderer;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager')
      ->getStorage('feeds_feed_type'), $container
      ->get('feeds_plugin_form_factory'), $container
      ->get('date.formatter'), $container
      ->get('renderer'));
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form['#tree'] = TRUE;
    $values = $form_state
      ->getValues();
    $form['#attached']['library'][] = 'feeds/feeds';
    $form['basics'] = [
      '#title' => $this
        ->t('Basic settings'),
      '#type' => 'details',
      '#open' => $this->entity
        ->isNew(),
      '#tree' => FALSE,
    ];
    $form['basics']['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Name'),
      '#default_value' => $this->entity
        ->label(),
      '#maxlength' => '255',
      '#description' => $this
        ->t('A unique label for this feed type. This label will be displayed in the interface.'),
      '#required' => TRUE,
    ];
    $form['basics']['id'] = [
      '#type' => 'machine_name',
      '#title' => $this
        ->t('Machine name'),
      '#default_value' => $this->entity
        ->id(),
      '#disabled' => !$this->entity
        ->isNew(),
      '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
      '#description' => $this
        ->t('A unique name for this feed type. It must only contain lowercase letters, numbers and underscores.'),
      '#machine_name' => [
        'exists' => 'Drupal\\feeds\\Entity\\FeedType::load',
        'source' => [
          'basics',
          'label',
        ],
      ],
      '#required' => TRUE,
    ];
    $form['basics']['description'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Description'),
      '#description' => $this
        ->t('A description of this feed type.'),
      '#default_value' => $this->entity
        ->getDescription(),
    ];
    $form['basics']['help'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Explanation or submission guidelines'),
      '#default_value' => $this->entity
        ->getHelp(),
      '#description' => $this
        ->t('This text will be displayed at the top of the feed when creating or editing a feed of this type.'),
    ];
    $form['plugin_settings'] = [
      '#type' => 'vertical_tabs',
      '#weight' => 99,
    ];
    $form['plugin_settings']['#prefix'] = '<div id="feeds-ajax-form-wrapper" class="feeds-feed-type-secondary-settings">';
    $form['plugin_settings']['#suffix'] = '</div>';
    $form['feed_type_settings'] = [
      '#type' => 'details',
      '#group' => 'plugin_settings',
      '#title' => $this
        ->t('Settings'),
      '#tree' => FALSE,
    ];
    $times = [
      900,
      1800,
      3600,
      10800,
      21600,
      43200,
      86400,
      259200,
      604800,
      2419200,
    ];
    $period = array_map(function ($time) {
      return $this->dateFormatter
        ->formatInterval($time);
    }, array_combine($times, $times));
    foreach ($period as &$p) {
      $p = $this
        ->t('Every @p', [
        '@p' => $p,
      ]);
    }
    $period = [
      FeedTypeInterface::SCHEDULE_NEVER => $this
        ->t('Off'),
      FeedTypeInterface::SCHEDULE_CONTINUOUSLY => $this
        ->t('As often as possible'),
    ] + $period;
    $cron_required = [
      '#type' => 'link',
      '#url' => Url::fromUri('https://www.drupal.org/docs/user_guide/en/security-cron.html'),
      '#title' => $this
        ->t('Requires cron to be configured.'),
      '#attributes' => [
        'target' => '_new',
      ],
    ];
    $form['feed_type_settings']['import_period'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Import period'),
      '#options' => $period,
      '#description' => $this
        ->t('Choose how often a feed should be imported.') . ' ' . $this->renderer
        ->renderRoot($cron_required),
      '#default_value' => $this->entity
        ->getImportPeriod(),
    ];
    foreach ($this->entity
      ->getPlugins() as $type => $plugin) {
      $options = $this->entity
        ->getPluginOptionsList($type);
      natcasesort($options);
      $form[$type . '_wrapper'] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'feeds-plugin-inline',
          ],
        ],
      ];
      if (count($options) === 1) {
        $form[$type . '_wrapper']['id'] = [
          '#type' => 'value',
          '#value' => $plugin
            ->getPluginId(),
          '#plugin_type' => $type,
          '#parents' => [
            $type,
          ],
        ];
      }
      else {
        $form[$type . '_wrapper']['id'] = [
          '#type' => 'select',
          '#title' => $this
            ->t('@type', [
            '@type' => ucfirst($type),
          ]),
          '#options' => $options,
          '#default_value' => $plugin
            ->getPluginId(),
          '#ajax' => [
            'callback' => '::ajaxCallback',
            'wrapper' => 'feeds-ajax-form-wrapper',
            'progress' => 'none',
          ],
          '#plugin_type' => $type,
          '#parents' => [
            $type,
          ],
        ];
      }

      // Give lockable plugins a chance to lock themselves.
      // @see \Drupal\feeds\Feeds\Processor\EntityProcessor::isLocked()
      if ($plugin instanceof LockableInterface) {
        $form[$type . '_wrapper']['id']['#disabled'] = $plugin
          ->isLocked();
      }
      $plugin_state = $this
        ->createSubFormState($type . '_configuration', $form_state);

      // This is the small form that appears under the select box.
      if ($this
        ->pluginHasForm($plugin, 'option')) {
        $option_form = $this->formFactory
          ->createInstance($plugin, 'option');
        $form[$type . '_wrapper']['advanced'] = $option_form
          ->buildConfigurationForm([], $plugin_state);
      }
      $form[$type . '_wrapper']['advanced']['#prefix'] = '<div id="feeds-plugin-' . $type . '-advanced">';
      $form[$type . '_wrapper']['advanced']['#suffix'] = '</div>';
      if ($this
        ->pluginHasForm($plugin, 'configuration')) {
        $form_builder = $this->formFactory
          ->createInstance($plugin, 'configuration');
        $plugin_form = $form_builder
          ->buildConfigurationForm([], $plugin_state);
        $form[$type . '_configuration'] = [
          '#type' => 'details',
          '#group' => 'plugin_settings',
          '#title' => $this
            ->t('@type settings', [
            '@type' => ucfirst($type),
          ]),
        ];
        $form[$type . '_configuration'] += $plugin_form;
      }
    }
    $form_state
      ->setValue($type . '_configuration', $plugin_state
      ->getValues());
    return parent::form($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  protected function actions(array $form, FormStateInterface $form_state) {
    $actions = parent::actions($form, $form_state);
    if ($this->entity
      ->isNew()) {
      $actions['submit']['#value'] = $this
        ->t('Save and add mappings');
      $actions['submit']['#submit'][] = '::toMapping';
    }
    else {
      $actions['submit']['#value'] = $this
        ->t('Save feed type');
    }
    return $actions;
  }

  /**
   * Returns the plugin forms for this feed type.
   *
   * @return \Drupal\feeds\Plugin\Type\ExternalPluginFormInterface[]
   *   A list of form objects, keyed by plugin id.
   */
  protected function getPluginForms() {
    $plugins = [];
    foreach ($this->entity
      ->getPlugins() as $type => $plugin) {
      if ($this
        ->pluginHasForm($plugin, 'configuration')) {
        $plugins[$type] = $this->formFactory
          ->createInstance($plugin, 'configuration');
      }
    }
    return $plugins;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if ($form_state
      ->getErrors()) {
      return;
    }
    $values =& $form_state
      ->getValues();

    // Moved advanced settings to regular settings.
    foreach (array_keys($this->entity
      ->getPlugins()) as $type) {
      if (isset($values[$type . '_wrapper']['advanced'])) {
        if (!isset($values[$type . '_configuration'])) {
          $values[$type . '_configuration'] = [];
        }
        $values[$type . '_configuration'] += $values[$type . '_wrapper']['advanced'];
      }
      unset($values[$type . '_wrapper']);
    }
    foreach ($this
      ->getPluginForms() as $type => $plugin) {
      if (!isset($form[$type . '_configuration'])) {

        // When switching from a non-configurable plugin to a configurable
        // plugin, no form is yet available. So skip validating it to avoid
        // fatal errors.
        continue;
      }
      $plugin_state = $this
        ->createSubFormState($type . '_configuration', $form_state);
      $plugin
        ->validateConfigurationForm($form[$type . '_configuration'], $plugin_state);
      $form_state
        ->setValue($type . '_configuration', $plugin_state
        ->getValues());
      $this
        ->moveFormStateErrors($plugin_state, $form_state);
    }

    // Build the feed type object from the submitted values.
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    foreach ($this
      ->getPluginForms() as $type => $plugin) {
      $plugin_state = $this
        ->createSubFormState($type . '_configuration', $form_state);
      $plugin
        ->submitConfigurationForm($form[$type . '_configuration'], $plugin_state);
      $form_state
        ->setValue($type . '_configuration', $plugin_state
        ->getValues());
    }
    parent::submitForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    $this->entity
      ->save();
    $form_state
      ->setRedirect('entity.feeds_feed_type.edit_form', [
      'feeds_feed_type' => $this->entity
        ->id(),
    ]);
    $this
      ->messenger()
      ->addStatus($this
      ->t('Your changes have been saved.'));
  }

  /**
   * {@inheritdoc}
   */
  public function toMapping(array &$form, FormStateInterface $form_state) {
    $form_state
      ->setRedirectUrl($this->entity
      ->toUrl('mapping'));
  }

  /**
   * Sends an ajax response.
   */
  public function ajaxCallback(array $form, FormStateInterface $form_state) {
    $type = $form_state
      ->getTriggeringElement()['#plugin_type'];
    $response = new AjaxResponse();

    // Set URL hash so that the correct settings tab is open.
    if (isset($form[$type . '_configuration']['#id'])) {
      $hash = ltrim($form[$type . '_configuration']['#id'], '#');
      $response
        ->addCommand(new SetHashCommand($hash));
    }

    // Update the forms.
    $plugin_settings = $this->renderer
      ->renderRoot($form['plugin_settings']);
    $advanced_settings = $this->renderer
      ->renderRoot($form[$type . '_wrapper']['advanced']);
    $response
      ->addCommand(new ReplaceCommand('#feeds-ajax-form-wrapper', $plugin_settings));
    $response
      ->addCommand(new ReplaceCommand('#feeds-plugin-' . $type . '-advanced', $advanced_settings));

    // Add attachments.
    $attachments = NestedArray::mergeDeep($form['plugin_settings']['#attached'], $form[$type . '_wrapper']['advanced']['#attached']);
    $response
      ->setAttachments($attachments);

    // Display status messages.
    $status_messages = [
      '#type' => 'status_messages',
    ];
    $output = $this->renderer
      ->renderRoot($status_messages);
    if (!empty($output)) {
      $response
        ->addCommand(new HtmlCommand('.region-messages', $output));
    }
    return $response;
  }

  /**
   * Creates a FormStateInterface object for a plugin.
   *
   * @param string|array $key
   *   The form state key.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state to copy values from.
   *
   * @return \Drupal\Core\Form\FormStateInterface
   *   A new form state object.
   *
   * @see FormStateInterface::getValue()
   */
  protected function createSubFormState($key, FormStateInterface $form_state) {

    // There might turn out to be other things that need to be copied and passed
    // into plugins. This works for now.
    return (new FormState())
      ->setValues($form_state
      ->getValue($key, []));
  }

  /**
   * Moves form state errors from one form state to another.
   */
  protected function moveFormStateErrors(FormStateInterface $from, FormStateInterface $to) {
    foreach ($from
      ->getErrors() as $name => $error) {
      $to
        ->setErrorByName($name, $error);
    }
  }

  /**
   * Returns whether or not the plugin implements a form for the given type.
   *
   * @param \Drupal\feeds\Plugin\Type\FeedsPluginInterface $plugin
   *   The Feeds plugin.
   * @param string $operation
   *   The type of form to check for. See
   *   \Drupal\feeds\Plugin\PluginFormFactory::hasForm() for more information.
   *
   * @return bool
   *   True if the plugin implements a form of the given type. False otherwise.
   *
   * @see \Drupal\feeds\Plugin\PluginFormFactory::hasForm()
   */
  protected function pluginHasForm(FeedsPluginInterface $plugin, $operation) {
    return $this->formFactory
      ->hasForm($plugin, $operation);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EntityForm::$entity protected property The entity being used by this form. 7
EntityForm::$entityTypeManager protected property The entity type manager. 3
EntityForm::$moduleHandler protected property The module handler service.
EntityForm::$operation protected property The name of the current operation.
EntityForm::$privateEntityManager private property The entity manager.
EntityForm::actionsElement protected function Returns the action form element for the current entity form.
EntityForm::afterBuild public function Form element #after_build callback: Updates the entity with submitted data.
EntityForm::buildEntity public function Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface::buildEntity 2
EntityForm::buildForm public function Form constructor. Overrides FormInterface::buildForm 10
EntityForm::copyFormValuesToEntity protected function Copies top-level form values to entity properties 7
EntityForm::getBaseFormId public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormId 5
EntityForm::getEntity public function Gets the form entity. Overrides EntityFormInterface::getEntity
EntityForm::getEntityFromRouteMatch public function Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface::getEntityFromRouteMatch 1
EntityForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId 10
EntityForm::getOperation public function Gets the operation identifying the form. Overrides EntityFormInterface::getOperation
EntityForm::init protected function Initialize the form state and the entity before the first form build. 3
EntityForm::prepareEntity protected function Prepares the entity object before the form is built first. 3
EntityForm::prepareInvokeAll protected function Invokes the specified prepare hook variant.
EntityForm::processForm public function Process callback: assigns weights and hides extra fields.
EntityForm::setEntity public function Sets the form entity. Overrides EntityFormInterface::setEntity
EntityForm::setEntityManager public function Sets the entity manager for this form. Overrides EntityFormInterface::setEntityManager
EntityForm::setEntityTypeManager public function Sets the entity type manager for this form. Overrides EntityFormInterface::setEntityTypeManager
EntityForm::setModuleHandler public function Sets the module handler for this form. Overrides EntityFormInterface::setModuleHandler
EntityForm::setOperation public function Sets the operation for this form. Overrides EntityFormInterface::setOperation
EntityForm::__get public function
EntityForm::__set public function
FeedTypeForm::$dateFormatter protected property Provides a service to handle various date related functionality.
FeedTypeForm::$feedTypeStorage protected property The feed type storage controller.
FeedTypeForm::$formFactory protected property The form factory.
FeedTypeForm::$renderer protected property Turns a render array into a HTML string.
FeedTypeForm::actions protected function Returns an array of supported actions for the current entity form. Overrides EntityForm::actions
FeedTypeForm::ajaxCallback public function Sends an ajax response.
FeedTypeForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
FeedTypeForm::createSubFormState protected function Creates a FormStateInterface object for a plugin.
FeedTypeForm::form public function Gets the actual form array to be built. Overrides EntityForm::form
FeedTypeForm::getPluginForms protected function Returns the plugin forms for this feed type.
FeedTypeForm::moveFormStateErrors protected function Moves form state errors from one form state to another.
FeedTypeForm::pluginHasForm protected function Returns whether or not the plugin implements a form for the given type.
FeedTypeForm::save public function Form submission handler for the 'save' action. Overrides EntityForm::save
FeedTypeForm::submitForm public function This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state… Overrides EntityForm::submitForm
FeedTypeForm::toMapping public function
FeedTypeForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
FeedTypeForm::__construct public function Constructs a new FeedTypeForm object.
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.