1

I created a module that creates a block to show a schedule information. I need taxonomy terms to configure the block. I want that those terms are filled with preconfigured values when I install the module.

I have in my config/install folder the definition of my taxonomy:

core.entity_form_display.taxonomy_term.schedule_channels.default.yml
core.entity_view_display.taxonomy_term.schedule_channels.default.yml
field.field.taxonomy_term.schedule_channels.field_channel_id.yml
field.storage.taxonomy_term.field_channel_id.yml
taxonomy.vocabulary.schedule_channels.yml

Here the content of the file field.storage.taxonomy_term.field_channel_id.yml:

langcode: en
status: true
dependencies:
  module:
    - taxonomy
id: taxonomy_term.field_channel_id
field_name: field_channel_id
entity_type: taxonomy_term
type: string
settings:
  max_length: 50
  is_ascii: false
  case_sensitive: false
module: core
locked: false
cardinality: 1
translatable: true
indexes: {  }
persist_with_no_fields: false
custom_storage: false

Also in my schedule_block.install I have:

class schedule_block_class{

  const schedule_block_channels_terms = array(
    array(
      'name' => 'Brava',
      'field_channel_id' => 'BRA001'
    ),
    array(
      'name' => 'DJazz',
      'field_channel_id' => 'DJA001'
    ));
  const schedule_block_channels_vid = 'schedule_channels';
}

function schedule_block_install(){
  $schedule_block_terms = schedule_block_class::schedule_block_channels_terms;
  $schedule_block_vid = schedule_block_class::schedule_block_channels_vid;
  foreach($schedule_block_terms as $schedule_block_term){
    $term = Term::create(array(
      'name' => $schedule_block_term['name'],
      'field_channel_id' => $schedule_block_term['field_channel_id'],
      'description' => '',
      'parent' => array(0),
      'vid' => $schedule_block_vid
    ));
    $term->save();
  }
}

function schedule_block_uninstall(){

  $schedule_block_terms = schedule_block_class::schedule_block_channels_terms;
  $schedule_block_vid = schedule_block_class::schedule_block_channels_vid;
  $terms = array();
  foreach($schedule_block_terms as $schedule_block_term){
    if($terms_name = taxonomy_term_load_multiple_by_name($schedule_block_term['name'], $schedule_block_vid)){
      $terms = array_merge($terms, $terms_name);
    }
  }
  foreach($terms as $term){
    $term->delete();
  }
  $taxo = \Drupal\taxonomy\Entity\Vocabulary::load(schedule_block_class::schedule_block_channels_vid);
  if(!is_null($taxo)){
    $taxo->delete();
  }
}

When I install my module, my taxonomy vocabulary is created and my terms are loaded with my values

... BUT ...

After about 24h the values have disappeared. Checking in the table taxonomy_term__field_channel_id confirms that there is no more any values. I still have my taxo vocab, and all the needed tables are present.

Why the values are not permanent?

2
  • I've recently seen a problem with drupal thinking that a field got deleted and it constantly does cleanup when cron runs. Check the keys for deleted fields and storages in state (key value table) – Berdir Aug 8 '16 at 18:28
  • @Berdir Yes in the key_value table in the row state/field.field.deleted and the row state/field.storage.deleted I see my field names in the value column. – Guillaume Bois Aug 8 '16 at 19:20
0

(Re comments)

That's the reason then. Those fields being in state, which means drupal thinks they were deleted and is trying to delete conent in them.

Remove them from there. Probably OK to drop the whole rows, check all the other fields in there. You might have some deleted field tables that you need to clean up manually.

I've seen someone else have this problem, what we couldn't figure out is how it happened. Please open a core issue (probably critical) and try to describe what you did.. delete fields, config import, create through manual config import. Anything you can think of that might have caused this.

2
  • Ok that is what is happening: When developing a module .install you often have to uninstall/reinstall. When you uninstall the module, the fields are marked for deletion (see key_value table at the row with collection to state and field.field.deleted and field.storage.deleted). If you reinstall your module without running the cron manually, your fields values will be deleted at the next cron pass. I don't know if you could consider this a bug. – Guillaume Bois Aug 9 '16 at 13:04
  • Yes, that's definitely a bug. it is supposed to work because the table is renamed on uninstall and then it deletes from that. But somehow that doesn't happen and it deletes from the real table. – Berdir Aug 9 '16 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.