4

Maintainer of Custom Publishing Options here... trying to figure out how to port to 8 in a clean way.

https://www.drupal.org/node/2647410

I have config entities working, so I can define custom publish options in the admin.

From here, I would like to add these options on the Node form(s), but they also need to be stored in node_field_data table. Previously hook_schema_alter / entity_info_alter was used to do this, example below:

D7:

/**
 * Implements hook_schema_alter().
 */
function custom_pub_schema_alter(&$schema) {
  $types = variable_get('custom_pub_types', array());
  foreach ($types as $type) {
    $schema['node']['fields'][$type['type']] = array(
      'description' => t('Custom publishing option @name', array('@name' => $type['name'])),
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
    );
  }
}

D8:

function custom_pub_entity_base_field_info(EntityTypeInterface $entity_type) {
  if ($entity_type->id() === 'node') {
    $fields = [];

    foreach (Drupal::entityTypeManager()->getStorage('custom_publishing_option')->loadMultiple() as $machine_name => $publish_option) {
      $fields['cpo_' . $publish_option->id()] = BaseFieldDefinition::create('boolean')
        ->setLabel(t('!label', ['!label' => $publish_option->label()]))
        ->setDescription(t('A status added by custom publishing options.'))
        ->setRevisionable(TRUE)
        ->setTranslatable(TRUE)
        ->setDefaultValue(FALSE)
        ->setDisplayConfigurable('form', TRUE);
    }

    return $fields;
  }
}

$fields looks good here, and no errors are thrown - yet, I do not see any change to the data in the database. I would expect fields to be added to the node_field_data table, what is missing here? hook_schema_alter() has been removed, is there a way to trigger this to update that I don't see?

The purpose of this module is to let admins define their own statuses to use with the default node statuses, which from there they can combine them for different Views/Rules/Action capabilities. Being set on the node similar to status/promote/sticky made it fairly quick.

6
  • I am sorry but you need to ask something more clear (on hop on IRC so we can have an iterative conversation) -- I do not know what you'd like to do.
    – user49
    Commented Jan 31, 2016 at 2:55
  • Updating OG question.
    – Kevin
    Commented Feb 2, 2016 at 17:32
  • You need to create the fields in a install hook / update hook, like \node_update_8001() does for example Commented Feb 2, 2016 at 17:51
  • These are declared by the user though - in the admin interface.
    – Kevin
    Commented Feb 2, 2016 at 18:05
  • What about doing that same code in postSave/postDelete of the ConfigEntityBase form for this config entity?
    – Kevin
    Commented Feb 2, 2016 at 18:45

1 Answer 1

3

When entities are created, I had to do this to create them in the node table:

$manager = \Drupal::entityDefinitionUpdateManager();

if ($is_new) {
      // create the field definition for the node
      $storage_definition = BaseFieldDefinition::create('boolean')
        ->setLabel(t('!label', ['!label' => $label]))
        ->setDescription(t('!description', ['!description' => $description]))
        ->setRevisionable(TRUE)
        ->setTranslatable(TRUE)
        ->setDefaultValue(0)
        ->setDisplayConfigurable('form', TRUE);

      $manager->installFieldStorageDefinition($id, 'node', 'custom_pub', $storage_definition);
    } else {
      $storage_definition = $manager->getFieldStorageDefinition($id, 'node');

      if (isset($storage_definition)) {
        // update the label and description on the definition
        $storage_definition
          ->setLabel(t('!label', ['!label' => $label]))
          ->setDescription(t('!description', ['!description' => $description]));

        $manager->updateFieldStorageDefinition($storage_definition);
      }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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