FormModeManagerConverter.php in Form mode manager 8
File
src/ParamConverter/FormModeManagerConverter.php
View source
<?php
namespace Drupal\form_mode_manager\ParamConverter;
use Drupal\Core\ParamConverter\ParamConverterInterface;
use Drupal\form_mode_manager\FormModeManagerInterface;
use Symfony\Component\Routing\Route;
class FormModeManagerConverter implements ParamConverterInterface {
protected $formModeManager;
public function __construct(FormModeManagerInterface $form_mode_manager) {
$this->formModeManager = $form_mode_manager;
}
public function convert($value, $definition, $name, array $defaults) {
$entity_type_id = $defaults['_route_object']
->getOption('_form_mode_manager_entity_type_id');
$form_mode_id = $entity_type_id . '.' . $value;
if ($form_mode_id === $defaults['_entity_form'] && $entity_type_id) {
return $this->formModeManager
->getFormModesByEntity($entity_type_id)[$value];
}
return $value;
}
public function applies($definition, $name, Route $route) {
if ('form_mode_name' === $name && (!empty($definition['type']) && 0 != preg_match('/^.*\\./', $definition['type']))) {
return TRUE;
}
return FALSE;
}
protected function getEntityForm(array $defaults, array $definition) {
$entity_form = $defaults['_entity_form'];
$form_mode_id = $defaults['_route_object']
->getOption('_form_mode_manager_entity_type_id') . '.' . $defaults['form_mode_name'];
if ($entity_form === $form_mode_id) {
return explode('.', $defaults['_entity_form'])[0];
}
return FALSE;
}
}