Skip to content
Snippets Groups Projects
Commit f1e36680 authored by Ahmad Alyasaki's avatar Ahmad Alyasaki
Browse files

Issue #3443512: Add Seeds Page Form settings

parent 0b5d7dfa
No related branches found
No related tags found
No related merge requests found
services:
seeds_page.manager:
class: Drupal\seeds_page\SeedsPageManager
arguments: ['@renderer','@image.factory','@current_route_match','@config.factory']
<?php
namespace Drupal\seeds_page\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\Entity\EntityTypeBundleInfo;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\AccessAwareRouter;
use Drupal\seeds_page\SeedsPageManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class SeedsPageForm.
*/
class SeedsPageForm extends ConfigFormBase {
/**
* Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManager
*/
protected $entityTypeManager;
/**
* Router.
*
* @var \Drupal\Core\Routing\AccessAwareRouter
*/
protected $router;
/**
* Bundle info.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfo
*/
protected $bundleInfo;
/**
* Seeds Page Manager.
*
* @var \Drupal\seeds_page\SeedsPageManager
*/
protected $seedsPageManager;
/**
*
*/
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManager $entity_type_manager, AccessAwareRouter $router, EntityTypeBundleInfo $bundle_info, SeedsPageManager $seeds_page_manager) {
parent::__construct($config_factory);
$this->entityTypeManager = $entity_type_manager;
$this->router = $router;
$this->bundleInfo = $bundle_info;
$this->seedsPageManager = $seeds_page_manager;
}
/**
*
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('entity_type.manager'),
$container->get('router'),
$container->get('entity_type.bundle.info'),
$container->get('seeds_page.manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'seeds_page_form';
}
/**
*
*/
public function getEditableConfigNames() {
return ['seeds_page.config'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('seeds_page.config');
$media = $this->entityTypeManager->getStorage('media')->load($config->get('media_id'));
$form['media_wrapper'] = [
'#type' => 'container',
];
$form['field'] = [
'#type' => 'select',
'#title' => $this->t("Field to Render"),
'#options' => $this->getMediaFields(),
'#default_value' => $config->get('field'),
];
$entity_types = $this->loadAllEntityTypes();
foreach ($entity_types as $entity_type_id => $entity_type) {
if ($this->seedsPageManager->isAcceptedEntity($entity_type_id)) {
$form['entity_types_wrapper'][$entity_type_id] = [
'#type' => 'checkboxes',
'#multiple' => TRUE,
'#title' => $entity_type['label'],
'#options' => $entity_type['bundles'],
'#default_value' => $config->get('entity_types')[$entity_type_id],
];
}
}
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
];
return $form;
}
/**
*
*/
private function loadAllEntityTypes() {
$entity_types = [];
$entity_definitions = \Drupal::entityTypeManager()->getDefinitions();
foreach ($entity_definitions as $entity_type_id => $entity_definition) {
if ($entity_definition instanceof ContentEntityType) {
// Check if the entity has a landing page by checking its route.
$route_name = "entity.$entity_type_id.canonical";
$has_landing_route = $this->router->getRouteCollection()->get($route_name);
if ($has_landing_route) {
$bundles = $this->bundleInfo->getBundleInfo($entity_type_id);
foreach ($bundles as $id => $bundle) {
$entity_types[$entity_type_id]['bundles'][$id] = $bundle['label'];
}
$entity_types[$entity_type_id]['label'] = $entity_definition->getLabel();
}
}
}
return $entity_types;
}
/**
*
*/
private function getMediaFields() {
$filtered_media_fields = [];
$media_fields = $this->entityTypeManager->getStorage('field_storage_config')->loadByProperties(
[
'settings' => [
'target_type' => 'media',
],
'type' => 'entity_reference',
'deleted' => FALSE,
'status' => 1,
]
);
foreach ($media_fields as $key => $field) {
$field_name = explode('.', $key)[1];
$filtered_media_fields[$field_name] = $field_name;
}
return $filtered_media_fields;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->configFactory->getEditable('seeds_page.config');
$config->set('entity_types', $form_state->getValue('entity_types_wrapper'));
$config->set('field', $form_state->getValue('field'));
$config->save();
parent::submitForm($form, $form_state);
}
}
<?php
namespace Drupal\seeds_page;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Image\ImageFactory;
use Drupal\Core\Render\Renderer;
use Drupal\Core\Routing\CurrentRouteMatch;
/**
*
*/
class SeedsPageManager {
/**
* Accepted entity types supported by Seeds to render the banner.
*/
protected const ACCEPTED_ENTITY_TYPES = [
'node',
'media',
'taxonomy_term',
'user',
];
/**
* Renderer.
*
* @var \Drupal\Core\Render\Renderer
*/
protected $renderer;
/**
* Image Factory.
*
* @var \Drupal\Core\Image\ImageFactory
*/
protected $imageFactory;
/**
* Current Route Match.
*
* @var \Drupal\Core\Routing\CurrentRouteMatch
*/
protected $routeMatch;
/**
* Config Factory definition.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Construct new SeedsPageManager object.
*/
public function __construct(
Renderer $renderer,
ImageFactory $image_factory,
CurrentRouteMatch $route_match,
ConfigFactoryInterface $config_factory
) {
$this->imageFactory = $image_factory;
$this->renderer = $renderer;
$this->routeMatch = $route_match;
$this->configFactory = $config_factory;
}
/**
* Checks if the passed entity type id is supported by Seeds.
*
* @param $entity_type_id
* The id of the entity type to be checked.
*
* @return bool
*/
public function isAcceptedEntity($entity_type_id) {
return in_array($entity_type_id, self::ACCEPTED_ENTITY_TYPES, TRUE);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment