You are here

function module_builder_entity_type_build in Module Builder 8.3

Implements hook_entity_type_build().

File

./module_builder.module, line 52
Builds scaffolding for custom modules.

Code

function module_builder_entity_type_build(array &$entity_types) {
  $entity_type_manager = \Drupal::service('entity_type.manager');

  // Process the 'code_builder' entity type annotation.
  foreach ($entity_types as $entity_type_id => $entity_type) {
    if (!$entity_type
      ->hasHandlerClass('component_sections')) {
      continue;
    }

    // Can't use $entity_type_manager->getHandler() here, as that will call
    // getDefinition() and create circularity.
    $handler_class = $entity_type
      ->getHandlerClass('component_sections');
    $component_sections_handler = $entity_type_manager
      ->createHandlerInstance($handler_class, $entity_type);

    // Expand form properties in the entity type annotation to set form classes,
    // entity links templates, etc.
    $form_operations = $component_sections_handler
      ->getFormOperations();

    // Set a generic form class for the 'misc' section if not specified.
    if (empty($entity_type
      ->getFormClass('misc'))) {
      $entity_type
        ->setFormClass('misc', \Drupal\module_builder\Form\ModuleMiscForm::class);
    }
    $canonical_template = $entity_type
      ->getLinkTemplate('canonical');
    foreach ($form_operations as $form_op) {

      // Allow the entity type to specify a form class.
      if (empty($entity_type
        ->getFormClass($form_op))) {
        $entity_type
          ->setFormClass($form_op, \Drupal\module_builder\Form\ComponentSectionForm::class);
      }

      // TODO: this is clobbering link templates in the entity if they are set
      // in the annotation -- bad DX! Either don't clobber, or complain.
      $entity_type
        ->setLinkTemplate("{$form_op}-form", $canonical_template . '/' . $form_op);
    }
  }
}