5

I've been able to successfully create a content type on the install of my module by following Creating a custom content type in Drupal 8 in the drupal.org documentation, but I can't find a way to add the publishing options.

screenshot

How should I add the publishing options when creating a content type?

1
  • I was going to write an answer, but I don't have all the information. The form to edit a content type (returned from NodeTypeForm::form()) seems to add those options unconditionally. I see that the Forum module is overriding the promote property for the forum content type, but I cannot say in which way that influences showing that options.
    – apaderno
    Aug 6 '17 at 0:43
3

You have to add a base field override to change the default value of status, promote and sticky for newly created nodes. For the first and second option the default value is predefined as TRUE and for the third option as FALSE in the base field definition of the entity type. See this example for promote:

Drupal\node\Entity\Node::baseFieldDefinitions

$fields['promote'] = BaseFieldDefinition::create('boolean')
  ->setLabel(t('Promoted to front page'))
  ->setRevisionable(TRUE)
  ->setTranslatable(TRUE)
  ->setDefaultValue(TRUE)
  ->setDisplayOptions('form', [
    'type' => 'boolean_checkbox',
    'settings' => [
      'display_label' => TRUE,
    ],
    'weight' => 15,
  ])
  ->setDisplayConfigurable('form', TRUE);

The base field override needs only to be present if you want to change this preset for a specific content type.

Example for the status field:

foobar/config/install/core.base_field_override.node.car_brand.status.yml

# core.base_field_override.node.car_brand.status.yml
langcode: en
status: true
dependencies:
  config:
    - node.type.car_brand
id: node.car_brand.status
field_name: status
entity_type: node
bundle: car_brand
label: 'Publishing status'
description: 'A boolean indicating the published state.'
required: false
translatable: true
default_value:
  -
    value: 0
default_value_callback: ''
settings:
  on_label: 'On'
  off_label: 'Off'
field_type: boolean

You can override the fields promote and sticky the same way.


The same task in PHP:

$fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('node', 'car_brand');
$fields['status']->getConfig('car_brand')
  ->setDefaultValue(FALSE)
  ->save();

The method BaseFieldDefinition::getConfig loads an existing base field override or creates one if none exists from the base field definition in the entity type.

Using the PHP code in an install hook is IMHO the better solution, because you only need to change the default value and don't need to repeat all other values.

11
  • NodeTypeForm::form() doesn't check the fields exist, when it adds the Publishing options part of the settings. Are you able to explain why it is necessary to add the fields to the node? Also, the Forum module just adds the Promote field.
    – apaderno
    Aug 7 '17 at 14:58
  • @kiamlaluno, this is an additional yaml file for the linked doc drupal.org/docs/8/api/entity-api/…. When you use this yaml file the first checkbox in the screenshot will be unchecked. Doing this for promote or sticky is for the second or third checkbox.
    – 4k4
    Aug 7 '17 at 16:05
  • Yes, but that doesn't say why it is necessary, considering the code used to show those field settings.
    – apaderno
    Aug 7 '17 at 18:25
  • @kiamlaluno, I don't understand. The question is about creating a content type on the install of a module by following the linked documentation, which describes how to do this by using yaml files. My answer is to override the default values by adding more yaml files. And this will show up in the Default Options where the red circle is.
    – 4k4
    Aug 7 '17 at 19:09
  • I understand the question and the answer. I was going to give a similar answer, but then I noticed I am not able to explain why that file is necessary, since the form builder for the node type forms is always showing those settings.
    – apaderno
    Aug 7 '17 at 19:10

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.