hasFormClasses example



  /** * Provides a list of eligible entity types for adding form modes. * * @return array * A list of entity types to add a form mode for. */
  public function formModeTypeSelection() {
    $entity_types = [];
    foreach ($this->entityTypeManager()->getDefinitions() as $entity_type_id => $entity_type) {
      if ($entity_type->get('field_ui_base_route') && $entity_type->hasFormClasses()) {
        $entity_types[$entity_type_id] = [
          'title' => $entity_type->getLabel(),
          'url' => Url::fromRoute('entity.entity_form_mode.add_form', ['entity_type_id' => $entity_type_id]),
          'localized_options' => [],
        ];
      }
    }
    return [
      '#theme' => 'admin_block_content',
      '#content' => $entity_types,
    ];
  }
$operation = 'default';
    $entity_type1 = $this->setUpEntityType([
      'handlers' => [
        'form' => [
          $operation => $controller,
        ],
      ],
    ]);
    $entity_type2 = $this->setUpEntityType([
      'handlers' => [],
    ]);
    $this->assertTrue($entity_type1->hasFormClasses());
    $this->assertFalse($entity_type2->hasFormClasses());
  }

  /** * Tests the getViewBuilderClass() method. */
  public function testGetViewBuilderClass() {
    $controller = $this->getTestHandlerClass();
    $entity_type = $this->setUpEntityType([
      'handlers' => [
        'view_builder' => $controller,
      ],

class EntityFormModeAddForm extends EntityDisplayModeAddForm {

  /** * {@inheritdoc} */
  protected function prepareEntity() {
    $definition = $this->entityTypeManager->getDefinition($this->targetEntityTypeId);
    if (!$definition->get('field_ui_base_route') || !$definition->hasFormClasses()) {
      throw new NotFoundHttpException();
    }

    $this->entity->setTargetType($this->targetEntityTypeId);
  }

}
/** * Filters entities based on their form mode handlers. * * @param $entity_type * The entity type of the entity that needs to be validated. * * @return bool * TRUE if the entity has any forms, FALSE otherwise. */
  protected function isValidEntity($entity_type) {
    return $this->entityTypes[$entity_type]->hasFormClasses();
  }

}
Home | Imprint | This part of the site doesn't use cookies.