You are here

class FormModeManagerConverter in Form mode manager 8

Converter for form_mode_manager routes.

This Converter is only used in add_page context.

Hierarchy

Expanded class hierarchy of FormModeManagerConverter

See also

\Drupal\form_mode_manager\Controller\EntityFormModeBase::addPage

1 string reference to 'FormModeManagerConverter'
form_mode_manager.services.yml in ./form_mode_manager.services.yml
form_mode_manager.services.yml
1 service uses FormModeManagerConverter
form_mode_manager.param_converter in ./form_mode_manager.services.yml
Drupal\form_mode_manager\ParamConverter\FormModeManagerConverter

File

src/ParamConverter/FormModeManagerConverter.php, line 16

Namespace

Drupal\form_mode_manager\ParamConverter
View source
class FormModeManagerConverter implements ParamConverterInterface {

  /**
   * The entity display repository.
   *
   * @var \Drupal\form_mode_manager\FormModeManagerInterface
   */
  protected $formModeManager;

  /**
   * Constructs a new FormModeManagerConverter.
   *
   * @param \Drupal\form_mode_manager\FormModeManagerInterface $form_mode_manager
   *   The form mode manager.
   */
  public function __construct(FormModeManagerInterface $form_mode_manager) {
    $this->formModeManager = $form_mode_manager;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function applies($definition, $name, Route $route) {
    if ('form_mode_name' === $name && (!empty($definition['type']) && 0 != preg_match('/^.*\\./', $definition['type']))) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Get the entity loaded into current route.
   *
   * If we are in classic case _entity_form are always here,
   * but in custom "list_page" route provide by FormModeManager,
   * We need to retrive the entity_type in "_route_object".
   *
   * @param array $defaults
   *   The route defaults array.
   * @param array $definition
   *   The parameter definition provided in the route options.
   *
   * @return string|false
   *   Extract the entity_type_id of current entity.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormModeManagerConverter::$formModeManager protected property The entity display repository.
FormModeManagerConverter::applies public function Determines if the converter applies to a specific route and variable. Overrides ParamConverterInterface::applies
FormModeManagerConverter::convert public function Converts path variables to their corresponding objects. Overrides ParamConverterInterface::convert
FormModeManagerConverter::getEntityForm protected function Get the entity loaded into current route.
FormModeManagerConverter::__construct public function Constructs a new FormModeManagerConverter.