Jump to content

ICT:Drupal Custom module design for automatic numbering: Difference between revisions

From Costa Sano MediaWiki
Created page with "= Automatic numbering of Asset records with custom desing module = == Environment premaration for custom modules = Custom modules live in <pre> /var/www/drupal/web/modules/custom$ </pre> Our custom module is called costasano_asset and as such a directory should be created for this module. Minimum 2 files are necessary for a custom module. <pre> /var/www/drupal/web/modules/custom/costasano_asset costasano_asset.info.yml costasano_asset.module </pre> A first file cost..."
 
No edit summary
Line 1: Line 1:
= Automatic numbering of Asset records with custom desing module =
= Automatic numbering of Asset records with custom desing module =


== Environment premaration for custom modules =
== Environment preparation for custom modules =


Custom modules live in  
Custom modules live in  
Line 30: Line 30:
drush en costasano_asset
drush en costasano_asset
</pre>
</pre>
== The module itself ==


The package = a php mpodule can then be written and step for step tested. After each step don't forget to clean the cash
The package = a php mpodule can then be written and step for step tested. After each step don't forget to clean the cash

Revision as of 15:24, 8 March 2026

Automatic numbering of Asset records with custom desing module

= Environment preparation for custom modules

Custom modules live in

/var/www/drupal/web/modules/custom$

Our custom module is called costasano_asset and as such a directory should be created for this module. Minimum 2 files are necessary for a custom module.

/var/www/drupal/web/modules/custom/costasano_asset
costasano_asset.info.yml
costasano_asset.module

A first file costasano_asset.info.yml defines the module inside the Drupal environment

name: Costasano Asset
type: module
description: Asset numbering logic for the Costasano Heritage Project
core_version_requirement: ^11
package: Costasano

Once this files exists, the module can be enabled:

drush en costasano_asset

The module itself

The package = a php mpodule can then be written and step for step tested. After each step don't forget to clean the cash

drush cr

The final code for the custom module costasano_asset.module is as follows:

/**
 * Implements hook_entity_presave().
 */
function costasano_asset_entity_presave(EntityInterface $entity) {

  if ($entity->getEntityTypeId() !== 'node') {
    return;
  }

  if ($entity->bundle() !== 'asset') {
    return;
  }

  if (!$entity->hasField('field_as_counter')) {
    return;
  }

  if ($entity->isNew()) {

    $connection = Database::getConnection();

    $max = $connection->select('node__field_as_counter', 'c')
      ->fields('c', ['field_as_counter_value'])
      ->orderBy('field_as_counter_value', 'DESC')
      ->range(0, 1)
      ->execute()
      ->fetchField();

    $counter = $max ? $max + 1 : 1;

    $entity->set('field_as_counter', $counter);

    $sequence = str_pad($counter, 5, '0', STR_PAD_LEFT);

    $chapter = $entity->get('field_as_chapter')->entity;
    $place = $entity->get('field_as_place')->entity;
    $organisation = $entity->get('field_as_organisation')->entity;

    $chapter_code = $chapter ? $chapter->label() : '';
    $context_code = '';

    if ($place) {
      $context_code = $place->label();
    }

    if ($organisation) {
      $context_code = $organisation->label();
    }

    $identifier = $chapter_code . '-' . $context_code . '-' . $sequence;

    $entity->setTitle($identifier);
  }
}

/**
 * Hide structural fields after creation.
 */
function costasano_asset_form_node_asset_edit_form_alter(&$form, FormStateInterface $form_state) {

  $node = $form_state->getFormObject()->getEntity();

  if (!$node->isNew()) {

    unset($form['field_as_chapter']);
    unset($form['field_as_place']);
    unset($form['field_as_organisation']);

  }

}