Ingredient.php in Recipe 8.2
File
modules/ingredient/src/Entity/Ingredient.php
View source
<?php
namespace Drupal\ingredient\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\ingredient\IngredientInterface;
class Ingredient extends ContentEntityBase implements IngredientInterface {
use EntityChangedTrait;
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function getChangedTime() {
return $this
->get('changed')->value;
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['id'] = BaseFieldDefinition::create('integer')
->setLabel(t('ID'))
->setDescription(t('The ID of the Ingredient entity.'))
->setReadOnly(TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the Ingredient entity.'))
->setReadOnly(TRUE);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the Ingredient entity.'))
->setRequired(TRUE)
->setTranslatable(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -6,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -6,
])
->setDisplayConfigurable('form', TRUE);
$fields['langcode'] = BaseFieldDefinition::create('language')
->setLabel(t('Language'))
->setDescription(t('The ingredient language code.'))
->setTranslatable(TRUE)
->setDisplayOptions('view', [
'region' => 'hidden',
])
->setDisplayOptions('form', [
'type' => 'language_select',
'weight' => 2,
]);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the ingredient was created.'))
->setTranslatable(TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the ingredient was last edited.'))
->setTranslatable(TRUE);
return $fields;
}
public function preSave(EntityStorageInterface $storage) {
$config = \Drupal::config('ingredient.settings');
if ($this
->isNew() && $config
->get('ingredient_name_normalize')) {
$name_field = $this
->get('name');
$values = $name_field
->getValue();
foreach ($values as $key => $value) {
if (!strpos($value['value'], '®')) {
$values[$key]['value'] = trim(strtolower($value['value']));
}
}
$name_field
->setValue($values, FALSE);
}
parent::preSave($storage);
}
}