simplepodcast-8.x-1.x-dev/src/Form/SimplePodcastAdminSettingsForm.php
src/Form/SimplePodcastAdminSettingsForm.php
<?php namespace Drupal\simplepodcast\Form; use Drupal\Core\Entity\EntityFieldManager; use Drupal\Core\Entity\EntityTypeManager; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\ProxyClass\Routing\RouteBuilder; use Drupal\simplepodcast\SimplePodcastCommonFunctions; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Configure simplepodcast settings for this site. */ class SimplePodcastAdminSettingsForm extends ConfigFormBase { /** * Config settings name. * * @var string */ const SETTINGS = 'simplepodcast.settings'; /** * EntityManager. * * @var \Drupal\Core\Entity\EntityManager */ private $entityTypeManager; /** * EntityFieldManager. * * @var \Drupal\Core\Entity\EntityFieldManager */ private $entityFieldManager; /** * RouteBuilder. * * @var \Drupal\Core\ProxyClass\Routing\RouteBuilder */ private $routeBuilder; /** * SimplePodcastAdminSettingsForm constructor. * * @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager * EntityTypeManager. * @param \Drupal\Core\Entity\EntityFieldManager $entityFieldManager * EntityFieldManager. * @param \Drupal\Core\ProxyClass\Routing\RouteBuilder $routeBuilder * RouteBuilder. */ public function __construct(EntityTypeManager $entityTypeManager, EntityFieldManager $entityFieldManager, RouteBuilder $routeBuilder) { $this->entityTypeManager = $entityTypeManager; $this->entityFieldManager = $entityFieldManager; $this->routeBuilder = $routeBuilder; } /** * {@inheritdoc} */ public function getFormId() { return 'simplepodcast_admin_settings'; } /** * {@inheritdoc} */ protected function getEditableConfigNames() { return [self::SETTINGS]; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( $container->get('entity_type.manager'), $container->get('entity_field.manager'), $container->get('router.builder') ); } /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $podcast_title = SimplePodcastCommonFunctions::getConfig('title'); $podcast_description = SimplePodcastCommonFunctions::getConfig('description'); $podcast_language = SimplePodcastCommonFunctions::getConfig('language'); $podcast_copyright = SimplePodcastCommonFunctions::getConfig('copyright'); $podcast_owner_name = SimplePodcastCommonFunctions::getConfig('owner_name'); $podcast_owner_author = SimplePodcastCommonFunctions::getConfig('owner_author'); $podcast_owner_email = SimplePodcastCommonFunctions::getConfig('owner_email'); $podcast_channel_image = SimplePodcastCommonFunctions::getConfig('channel_image'); $node_fields_array = $this->getNodeFields(); $form['general'] = [ '#type' => 'details', '#title' => $this->t('General settings'), '#open' => TRUE, ]; $form['general']['simplepodcast_content_type'] = [ '#default_value' => SimplePodcastCommonFunctions::getConfig('episode_content_type'), '#description' => $this->t('This is the machine name of your podcast content type'), '#required' => TRUE, '#title' => $this->t('Content Type'), '#type' => 'select', '#options' => $this->getNodeOptions(), ]; $form['general']['simplesimplepodcast_path_name'] = [ '#default_value' => SimplePodcastCommonFunctions::getConfig('rss_path_name'), '#description' => $this->t('This is the path (not including the hostname) of the RSS feed'), '#maxlength' => 32, '#required' => TRUE, '#size' => 15, '#title' => $this->t('RSS Path Name'), '#type' => 'textfield', ]; $form['general']['simplepodcast_title'] = [ '#default_value' => $this->t($podcast_title), '#description' => $this->t('This is the official title of the Podcast'), '#maxlength' => 32, '#required' => TRUE, '#size' => 15, '#title' => $this->t('Podcast Title'), '#type' => 'textfield', ]; $form['general']['simplepodcast_description'] = [ '#default_value' => $this->t($podcast_description), '#description' => $this->t('This is the official description of the Podcast'), '#required' => TRUE, '#rows' => 10, '#cols' => 80, '#resizable' => 'both', '#title' => $this->t('Podcast Description'), '#type' => 'textarea', ]; $form['general']['simplepodcast_language'] = [ '#default_value' => $this->t($podcast_language), '#description' => $this->t('This is the official language of the Podcast'), '#maxlength' => 8, '#required' => TRUE, '#size' => 15, '#title' => $this->t('Podcast Language'), '#type' => 'textfield', ]; $form['general']['simplepodcast_copyright'] = [ '#default_value' => $this->t($podcast_copyright), '#description' => $this->t('This is the copyright notice for the Podcast (not including copyright \'©\' symbol)'), '#maxlength' => 255, '#required' => TRUE, '#size' => 15, '#title' => $this->t('Podcast Copyright'), '#type' => 'textfield', ]; $form['general']['simplepodcast_explicit'] = [ '#default_value' => SimplePodcastCommonFunctions::getConfig('explicit'), '#return_value' => TRUE, '#title' => $this->t('Is Podcast Explicit?'), '#type' => 'checkbox', ]; $form['general']['simplepodcast_owner_name'] = [ '#default_value' => $this->t($podcast_owner_name), '#description' => $this->t('This is the owner name of the podcast'), '#maxlength' => 255, '#required' => TRUE, '#size' => 15, '#title' => $this->t('Podcast Owner Name'), '#type' => 'textfield', ]; $form['general']['simplepodcast_owner_author'] = [ '#default_value' => $this->t($podcast_owner_author), '#description' => $this->t('This is the author of the podcast'), '#maxlength' => 255, '#required' => TRUE, '#size' => 15, '#title' => $this->t('Podcast Owner Author'), '#type' => 'textfield', ]; $form['general']['simplepodcast_owner_email'] = [ '#default_value' => $podcast_owner_email, '#description' => $this->t('This is the owner email address of the podcast'), '#required' => TRUE, '#size' => 15, '#title' => $this->t('Podcast Owner Email Address'), '#type' => 'email', ]; $form['general']['simplepodcast_channel_image'] = [ '#default_value' => $this->t($podcast_channel_image), '#description' => $this->t('This is the channel image for iTunes and other podcast aggregators'), '#maxlength' => 255, '#required' => TRUE, '#size' => 15, '#title' => $this->t('Podcast Channel Image'), '#type' => 'textfield', ]; $form['general']['simplepodcast_category_1'] = [ '#default_value' => SimplePodcastCommonFunctions::getConfig('category_1'), '#description' => $this->t('This is the First iTunes Podcast Category. <strong>It cannot be blank!</strong>'), '#title' => $this->t('Category 1'), '#type' => 'select', '#options' => $this->getCategories(), ]; $form['general']['simplepodcast_category_2'] = [ '#default_value' => SimplePodcastCommonFunctions::getConfig('category_2'), '#description' => $this->t('This is the Second iTunes Podcast Category'), '#title' => $this->t('Category 2'), '#type' => 'select', '#options' => $this->getCategoriesWithBlank(), ]; $form['general']['simplepodcast_category_3'] = [ '#default_value' => SimplePodcastCommonFunctions::getConfig('category_3'), '#description' => $this->t('This is the Third iTunes Podcast Category'), '#title' => $this->t('Category 3'), '#type' => 'select', '#options' => $this->getCategoriesWithBlank(), ]; $form['general']['simplepodcast_item_title'] = [ '#default_value' => SimplePodcastCommonFunctions::getConfig('item_title'), '#description' => $this->t('This is the field from where the Episode Title should come.'), '#title' => $this->t('Episode Title'), '#type' => 'select', '#options' => $node_fields_array, ]; $form['general']['simplepodcast_item_author'] = [ '#default_value' => SimplePodcastCommonFunctions::getConfig('item_author'), '#description' => $this->t('This is the field from where the Episode Author should come.'), '#title' => $this->t('Episode Author'), '#type' => 'select', '#options' => $node_fields_array, ]; $form['general']['simplepodcast_item_subtitle'] = [ '#default_value' => SimplePodcastCommonFunctions::getConfig('item_subtitle'), '#description' => $this->t('This is the field from where the Episode Sub-Title should come.'), '#title' => $this->t('Episode Sub-Title'), '#type' => 'select', '#options' => $node_fields_array, ]; $form['general']['simplepodcast_item_summary'] = [ '#default_value' => SimplePodcastCommonFunctions::getConfig('item_summary'), '#description' => $this->t('This is the field from where the Episode Summary should come.'), '#title' => $this->t('Episode Summary'), '#type' => 'select', '#options' => $node_fields_array, ]; $form['general']['simplepodcast_item_image'] = [ '#default_value' => SimplePodcastCommonFunctions::getConfig('item_image'), '#description' => $this->t('This is the field from where the Episode Image should come.'), '#title' => $this->t('Episode Image'), '#type' => 'select', '#options' => $node_fields_array, ]; $form['general']['simplepodcast_item_media'] = [ '#default_value' => SimplePodcastCommonFunctions::getConfig('item_media'), '#description' => $this->t('This is the field from where the Episode Enclosure Media should come.'), '#title' => $this->t('Episode Media'), '#type' => 'select', '#options' => $node_fields_array, ]; $form['general']['simplepodcast_item_media_length'] = [ '#default_value' => SimplePodcastCommonFunctions::getConfig('item_media_length'), '#description' => $this->t('This is the field from where the length in bytes of the media should come.'), '#title' => $this->t('Episode Media Length'), '#type' => 'select', '#options' => $node_fields_array, ]; $form['general']['simplepodcast_item_media_duration'] = [ '#default_value' => SimplePodcastCommonFunctions::getConfig('item_media_duration'), '#description' => $this->t('This is the field from where the duration in seconds of the media should come.'), '#title' => $this->t('Episode Media Duration'), '#type' => 'select', '#options' => $node_fields_array, ]; return parent::buildForm($form, $form_state); } /** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { parent::validateForm($form, $form_state); if (!preg_match('/^[a-z0-9_]+$/', $form_state->getValue('simplepodcast_content_type'))) { $form_state->setErrorByName('simplepodcast_content_type', $this->t("A valid Drupal machine name is lowercase letters 'a-z', numbers '0-9', and underscores '_'")); } } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $config = $this->config(self::SETTINGS); $config ->set('episode_content_type', $form_state->getValue('simplepodcast_content_type')) ->set('rss_path_name', $form_state->getValue('simplesimplepodcast_path_name')) ->set('title', $form_state->getValue('simplepodcast_title')) ->set('description', $form_state->getValue('simplepodcast_description')) ->set('copyright', $form_state->getValue('simplepodcast_copyright')) ->set('explicit', $form_state->getValue('simplepodcast_explicit')) ->set('owner_name', $form_state->getValue('simplepodcast_owner_name')) ->set('owner_author', $form_state->getValue('simplepodcast_owner_author')) ->set('owner_email', $form_state->getValue('simplepodcast_owner_email')) ->set('channel_image', $form_state->getValue('simplepodcast_channel_image')) ->set('category_1', $form_state->getValue('simplepodcast_category_1')) ->set('category_2', $form_state->getValue('simplepodcast_category_2')) ->set('category_3', $form_state->getValue('simplepodcast_category_3')) ->set('item_title', $form_state->getValue('simplepodcast_item_title')) ->set('item_author', $form_state->getValue('simplepodcast_item_author')) ->set('item_subtitle', $form_state->getValue('simplepodcast_item_subtitle')) ->set('item_summary', $form_state->getValue('simplepodcast_item_summary')) ->set('item_image', $form_state->getValue('simplepodcast_item_image')) ->set('item_media', $form_state->getValue('simplepodcast_item_media')) ->set('item_media_length', $form_state->getValue('simplepodcast_item_media_length')) ->set('item_media_duration', $form_state->getValue('simplepodcast_item_media_duration')) ->save(); parent::submitForm($form, $form_state); $this->routeBuilder->rebuild(); } /** * Return the iTunes categories. * * @return array * An array of iTunes categories. */ private function getCategories() { return [ 'Arts' => [ 'Arts' => '--Arts--', 'Arts|Design' => 'Design', 'Arts|Fashion & Beauty' => 'Fashion & Beauty', 'Arts|Food' => 'Food', 'Arts|Literature' => 'Literature', 'Arts|Performing Arts' => 'Performing Arts', 'Arts|Visual Arts' => 'Visual Arts', ], 'Business' => [ 'Business' => '--Business--', 'Business|Business News' => 'Business News', 'Business|Careers' => 'Careers', 'Business|Investing' => 'Investing', 'Business|Management & Marketing' => 'Management & Marketing', 'Business|Shopping' => 'Shopping', ], 'Comedy' => ['Comedy' => '--Comedy--'], 'Education' => [ 'Education' => '--Education--', 'Education|Educational Technology' => 'Educational Technology', 'Education|Higher Education' => 'Higher Education', 'Education|K-12' => 'K-12', 'Education|Language Courses' => 'Language Courses', 'Education|Training' => 'Training', ], 'Games & Hobbies' => [ 'Games & Hobbies' => '--Games & Hobbies--', 'Games & Hobbies|Automotive' => 'Automotive', 'Games & Hobbies|Aviation' => 'Aviation', 'Games & Hobbies|Hobbies' => 'Hobbies', 'Games & Hobbies|Other Games' => 'Other Games', 'Games & Hobbies|Video Games' => 'Video Games', ], 'Government & Organizations' => [ 'Government & Organizations' => '--Government & Organizations--', 'Government & Organizations|Local' => 'Local', 'Government & Organizations|National' => 'National', 'Government & Organizations|Non-Profit' => 'Non-Profit', 'Government & Organizations|Regional' => 'Regional', ], 'Health' => [ 'Health' => '--Health--', 'Health|Alternative Health' => 'Alternative Health', 'Health|Fitness & Nutrition' => 'Fitness & Nutrition', 'Health|Self-Help' => 'Self-Help', 'Health|Sexuality' => 'Sexuality', ], 'Kids & Family' => ['Kids & Family' => '--Kids & Family--'], 'Music' => ['Music' => '--Music--'], 'News & Politics' => ['News & Politics' => '--News & Politics--'], 'Religion & Spirituality' => [ 'Religion & Spirituality' => '--Religion & Spirituality--', 'Religion & Spirituality|Buddhism' => 'Buddhism', 'Religion & Spirituality|Christianity' => 'Christianity', 'Religion & Spirituality|Hinduism' => 'Hinduism', 'Religion & Spirituality|Islam' => 'Islam', 'Religion & Spirituality|Judaism' => 'Judaism', 'Religion & Spirituality|Other' => 'Other', 'Religion & Spirituality|Spirituality' => 'Spirituality', ], 'Science & Medicine' => [ 'Science & Medicine' => '--Science & Medicine--', 'Science & Medicine|Medicine' => 'Medicine', 'Science & Medicine|Natural Sciences' => 'Natural Sciences', 'Science & Medicine|Social Sciences' => 'Social Sciences', ], 'Society & Culture' => [ 'Society & Culture' => '--Society & Culture--', 'Society & Culture|History' => 'History', 'Society & Culture|Personal Journals' => 'Personal Journals', 'Society & Culture|Philosophy' => 'Philosophy', 'Society & Culture|Places & Travel' => 'Places & Travel', ], 'Sports & Recreation' => [ 'Sports & Recreation|Amateur' => 'Amateur', 'Sports & Recreation|College & High School' => 'College & High School', 'Sports & Recreation|Outdoor' => 'Outdoor', 'Sports & Recreation|Professional' => 'Professional', ], 'Technology' => [ 'Technology|Gadgets' => 'Gadgets', 'Technology|Tech News' => 'Tech News', 'Technology|Podcasting' => 'Podcasting', 'Technology|Software How-To' => 'Software How-To', ], 'TV & Film' => ['TV & Film' => '--TV & Film--'], ]; } /** * Return the iTunes categories with a blank option. * * @return array * An array of iTunes categories with a blank option. */ private function getCategoriesWithBlank() { return array_merge(['' => 'NO CATEGORY'], $this->getCategories()); } /** * Return Node Options. * * @return array * Associative array of node ids and labels. * * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException */ private function getNodeOptions() { $node_options = []; $node_types = $this->entityTypeManager->getStorage('node_type')->loadMultiple(); // If you need to display them in a drop down: foreach ($node_types as $node_type) { $node_options[$node_type->id()] = $node_type->label(); } return $node_options; } /** * Return node fields. * * @return array * Array of node fields. */ private function getNodeFields() { $field_map = $this->entityFieldManager->getFieldMap(); $node_field_map = $field_map['node']; $node_fields = array_keys($node_field_map); $node_fields_array = []; foreach ($node_fields as $field) { $node_fields_array[$field] = $field; } return $node_fields_array; } }