module_builder.module in Module Builder 8.3
Same filename and directory in other branches
Builds scaffolding for custom modules.
File
module_builder.moduleView source
<?php
/**
* @file
* Builds scaffolding for custom modules.
*/
use Drupal\Core\Render\Element;
/**
* Implements hook_help().
*/
function module_builder_help($route_name, \Drupal\Core\Routing\RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.module_builder':
return t("Module builder allows you to generate code files for new custom modules.");
case 'entity.module_builder_module.collection':
return t('This page allows you to generate the files for a custom module with scaffolding code for hook implementations, plugins, and so on.');
}
}
/**
* Implements hook_theme().
*/
function module_builder_theme($existing, $type, $theme, $path) {
return [
'generated_files' => [
'render element' => 'element',
],
];
}
/**
* Prepares variables for generated files form element template.
*
* Default template: generated-files.html.twig.
*/
function template_preprocess_generated_files(&$variables) {
foreach (Element::children($variables['element']['filename_list']) as $filename) {
$variables['filename_list'][$filename] = $variables['element']['filename_list'][$filename];
}
foreach (Element::children($variables['element']['code']) as $filename) {
$variables['code'][$filename] = $variables['element']['code'][$filename];
}
}
/**
* Implements hook_entity_type_build().
*/
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);
}
}
}
Functions
Name | Description |
---|---|
module_builder_entity_type_build | Implements hook_entity_type_build(). |
module_builder_help | Implements hook_help(). |
module_builder_theme | Implements hook_theme(). |
template_preprocess_generated_files | Prepares variables for generated files form element template. |