0

I have two custom entities, which use the same boilerplate framework. I can add content to both, but one displays fine (sample) and the other is just blank (chemical).

The twig templates for both are:

module/templates/chemical.html.twig

<div{{ attributes.addClass('chemical') }}>
{% if content %}
  {{- content -}}
{% endif %}
</div>

and in module/templates/sample.html.twig

<div{{ attributes.addClass('sample') }}>
{% if content %}
  {{- content -}}
{% endif %}
</div>

There are no error messages. Is there something in the entity definition file module/src/Entity/Chemical.php that might be throwing off the display, but still allowing the data to be entered and saved correctly?

Here is the Chemical Entity definition:

<?php

namespace Drupal\lab_system\Entity;

use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;

/**
 * Defines the Chemical entity.
 *
 * @ingroup lab_system
 *
 * @ContentEntityType(
 *   id = "chemical",
 *   label = @Translation("Chemical"),
 *   handlers = {
 *     "view_builder" = "Drupal\Core\Entity\EntityViewBuilder",
 *     "list_builder" = "Drupal\lab_system\ChemicalListBuilder",
 *     "views_data" = "Drupal\lab_system\Entity\ChemicalViewsData",
 *
 *     "form" = {
 *       "default" = "Drupal\lab_system\Form\ChemicalForm",
 *       "add" = "Drupal\lab_system\Form\ChemicalForm",
 *       "edit" = "Drupal\lab_system\Form\ChemicalForm",
 *       "delete" = "Drupal\lab_system\Form\ChemicalDeleteForm",
 *     },
 *     "access" = "Drupal\lab_system\ChemicalAccessControlHandler",
 *     "route_provider" = {
 *       "html" = "Drupal\lab_system\ChemicalHtmlRouteProvider",
 *     },
 *   },
 *   base_table = "chemical",
 *   admin_permission = "administer chemical entities",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "name",
 *     "smiles" = "smiles",
 *     "boiling_point" = "boiling_point",
 *     "tid" = "tid",
 *     "alternate_tid" = "alternate_tid",
 *   },
 *   links = {
 *     "canonical" = "/admin/structure/lab_system/chemical/{chemical}",
 *     "add-form" = "/admin/structure/lab_system/chemical/add",
 *     "edit-form" = "/admin/structure/lab_system/chemical/{chemical}/edit",
 *     "delete-form" = "/admin/structure/lab_system/chemical/{chemical}/delete",
 *     "collection" = "/admin/structure/lab_system/chemical",
 *   },
 *   field_ui_base_route = "chemical.settings"
 * )
 */
class Chemical extends ContentEntityBase implements ChemicalInterface {

  /**
   * {@inheritdoc}
   */
  public function getAlternateNames() {
    return $this->get('alternate_names')->values;
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    return $this->get('name')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setName($name) {
    $this->set('name', $name);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getSmiles() {
    return $this->get('smiles')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function getBoilingPoint() {
    return $this->get('boiling_point')->value;
  }

  /**
   * {@inheritdoc}
   */
  public function setBoilingPoint($bp) {
    $this->set('boiling_point', $bp);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);

$fields['name'] = BaseFieldDefinition::create('string')
  ->setLabel(t('Name'))
  ->setDescription(t('The name of the Chemical entity.'))
  ->setSettings(array(
    'max_length' => 50,
    'text_processing' => 0,
  ))
  ->setDefaultValue('')
  ->setDisplayOptions('view', array(
    'label' => 'above',
    'type' => 'string',
    'weight' => -4,
  ))
  ->setDisplayOptions('form', array(
    'type' => 'string_textfield',
    'weight' => -4,
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

$fields['alternate_names'] = BaseFieldDefinition::create('string')
  ->setLabel(t('Alternate Name'))
  ->setDescription(t('Alternate Names.'))
  ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
  ->setSettings(array(
    'max_length' => 50,
    'text_processing' => 0,
  ))
  ->setDefaultValue('')
  ->setDisplayOptions('view', array(
    'label' => 'above',
    'type' => 'string',
    'weight' => -4,
  ))
  ->setDisplayOptions('form', array(
    'type' => 'string_textfield',
    'weight' => -4,
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

$fields['smiles'] = BaseFieldDefinition::create('string')
  ->setLabel(t('SMILES'))
  ->setDescription(t('The SMILES structure.'))
  ->setSettings(array(
    'max_length' => 100,
    'text_processing' => 0,
  ))
  ->setDefaultValue('')
  ->setDisplayOptions('view', array(
    'label' => 'above',
    'type' => 'string',
    'weight' => -4,
  ))
  ->setDisplayOptions('form', array(
    'type' => 'string_textfield',
    'weight' => -4,
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

$fields['boiling_point'] = BaseFieldDefinition::create('float')
  ->setLabel(t('Boiling Point'))
  ->setDescription(t('The boiling point of the compound.'))
  ->setDefaultValue(TRUE)
  ->setDisplayOptions('view', array(
    'label' => 'above',
    'type' => 'float',
    'weight' => 0,
  ))
  ->setDisplayOptions('form', array(
    'type' => 'string_textfield',
    'weight' => -4,
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

$fields['tid'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Chemical Family'))
  ->setDescription(t('The family to which this chemical belongs.'))
  ->setSetting('target_type', 'taxonomy_term')
  ->setSetting('handler', 'default')
  ->setTranslatable(TRUE)
  ->setDisplayOptions('view', array(
    'label' => 'above',
    'type' => 'integer',
    'weight' => 0,
  ))
  ->setDisplayOptions('form', array(
    'type' => 'entity_reference_autocomplete',
    'weight' => 5,
    'settings' => array(
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'autocomplete_type' => 'tags',
      'placeholder' => '',
    ),
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

$fields['alternate_tid'] = BaseFieldDefinition::create('entity_reference')
  ->setLabel(t('Chemical Family'))
  ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
  ->setDescription(t('The family to which this chemical belongs.'))
  ->setSetting('target_type', 'taxonomy_term')
  ->setSetting('handler', 'default')
  ->setTranslatable(TRUE)
  ->setDisplayOptions('view', array(
    'label' => 'above',
    'type' => 'integer',
    'weight' => 0,
  ))
  ->setDisplayOptions('form', array(
    'type' => 'entity_reference_autocomplete',
    'weight' => 5,
    'settings' => array(
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'autocomplete_type' => 'tags',
      'placeholder' => '',
    ),
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

return $fields;
  }

}

1 Answer 1

3

Probably because you have no preprocess hooks for your template? And did you define the templates with hook_theme()? You probably have render element => 'elements' there, but that means your output is actually in elements, not in content.

See https://www.drupal.org/node/2808481 for an issue to add generic entity templates to all entities with everything that's needed, that will also show you what you currently need to do yourself to get something in {{ content }}

2
  • This fixed it...kind of. The problem it that there IS a definition in the hook that was empty. Removing it fixed the problem. The entity definition must register the template on its own somehow. Commented Dec 29, 2016 at 23:25
  • 1
    templates are not registered on their own. If you removed the hook then that simply means that no template is used now and it is displayed as a plain render array by simply rendering and displaying its children. That a) currently results in watchdog messages and b) changing your template will obviously not do anything.
    – Berdir
    Commented Dec 29, 2016 at 23:52

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.