apigee_m10n-8.x-1.7/src/Form/RatePlanXConfigForm.php
src/Form/RatePlanXConfigForm.php
<?php
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Drupal\apigee_m10n\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class for managing `apigee_m10n.xrate_plan.config` settings.
*/
class RatePlanXConfigForm extends ConfigFormBase {
/**
* The config named used by this form.
*/
const CONFIG_NAME = 'apigee_m10n.xrate_plan.config';
/**
* The entity display repository.
*
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
*/
protected $entity_display_repository;
/**
* Constructs a \Drupal\apigee_m10n\Form\RatePlanXConfigForm object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
* The entity display repository.
* @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config_manager
* The typed config manager.
*/
public function __construct(ConfigFactoryInterface $config_factory, EntityDisplayRepositoryInterface $entity_display_repository, TypedConfigManagerInterface $typed_config_manager) {
parent::__construct($config_factory, $typed_config_manager);
$this->entity_display_repository = $entity_display_repository;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('entity_display.repository'),
$container->get('config.typed')
);
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [static::CONFIG_NAME];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'rate_plan_x_settings';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Get the working configuration.
$config = $this->config(static::CONFIG_NAME);
// Get view mode options from the repository service.
$options = $this->entity_display_repository->getViewModeOptionsByBundle('rate_plan', 'rate_plan');
$form['catalog_view_mode'] = [
'#type' => 'select',
'#title' => $this->t('Catalog view mode'),
'#description' => $this->t('View mode to use for rate plans on the "Buy Apis" page.'),
'#options' => $options,
'#default_value' => $config->get('catalog_view_mode'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this->config(static::CONFIG_NAME)
->set('catalog_view_mode', $form_state->getValue('catalog_view_mode'))
->save();
}
}
