Custom Module Help

Going to attempt to make a custom module,
Initially I think I need some additional flags and data fields for Cattle.

I started with some flags, which seem to work but have generated warnings.

User warning: The configuration name "farm_flag.flag.bull" does not match the ID "flag_bull" in Drupal\Core\Config\ConfigInstaller->createConfiguration() (line 394 of core/lib/Drupal/Core/Config/ConfigInstaller.php).
Drupal\Core\Config\ConfigInstaller->createConfiguration('', Array) (Line: 152)
Drupal\Core\Config\ConfigInstaller->installDefaultConfig('module', 'farm_cattle_flags') (Line: 75)
Drupal\Core\ProxyClass\Config\ConfigInstaller->installDefaultConfig('module', 'farm_cattle_flags') (Line: 324)
Drupal\Core\Extension\ModuleInstaller->install(Array, 1) (Line: 83)
Drupal\Core\ProxyClass\Extension\ModuleInstaller->install(Array) (Line: 476)
Drupal\system\Form\ModulesListForm->submitForm(Array, Object)
call_user_func_array(Array, Array) (Line: 114)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 52)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 592)
Drupal\Core\Form\FormBuilder->processForm('system_modules', Array, Object) (Line: 320)
Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 564)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 158)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 67)
Drupal\simple_oauth\HttpMiddleware\BasicAuthSwap->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 708)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Is there a naming convention for the ID’s? I’ve tried a few combinations of config name and ID including having them the same.

This is the current version:
farm_flag.flag.bull.yml

angcode: en
status: true
dependencies:
  enforced:
    module:
      - farm_cattle_flags
id: flag_bull
label: Stock Bull
entity_types: null


entity_types:
  asset:
    - animal

@Farmer-Ed Change id: flag_bull to id: bull - the file name needs to match the id declared in YML. So if the file is named farm_flag.flag.bull.yml then the id should be bull. The pattern is farm_flag.flag.[id].yml.

2 Likes

That fixed it alright, Thanks.

I was sure that was the combination I started with, but perhaps I used a capital letter.

1 Like

Is there an easy way to add colour to tags? or does it require some additional PHP/CSS?

In case you don’t understand my accent, some color would do either :smiley:
It’s not really a big issue as the additional tags already make searching for and organizing records much easier, but maybe being able to highlight a few would be good too.

1 Like

Is there an easy way to add colour to tags? or does it require some additional PHP/CSS?

You need to do PHP/CSS yourself. Nothing unified yet - but we have a tracking issue open for this!

To style a flag named myflag you need three things:

  1. A CSS file in your module (eg: css/flag.css)
    .flag--myflag {
      background-color: #F00;
      border-color: #F00;
    }
    
  2. A libraries YML file (eg: mymodule.libraries.yml):
    flag:
      css:
        theme:
          css/flag.css: { }
    
  3. An implementation of hook_preprocess_field__flag that adds your CSS whenever flags are displayed on the page (eg: mymodule.module):
    /**
     * Implements hook_preprocess_HOOK().
     */
    function mymodule_preprocess_field__flag(array &$variables) {
      $variables['#attached']['library'][] = 'mymodule/flag';
    }
    
1 Like

Ah nice!

I’ll give that a go, thanks.

1 Like