public function SocialLinkItem::preSave in Social Link Field 8
Defines custom presave behavior for field values.
This method is called during the process of saving an entity, just before values are written into storage. When storing a new entity, its identifier will not be available yet. This should be used to massage item property values or perform any other operation that needs to happen before values are stored. For instance this is the proper phase to auto-create a new entity for an entity reference field item, because this way it will be possible to store the referenced entity identifier.
Overrides FieldItemBase::preSave
File
- src/
Plugin/ Field/ FieldType/ SocialLinkItem.php, line 69
Class
- SocialLinkItem
- Defines the 'social_links' entity field type.
Namespace
Drupal\social_link_field\Plugin\Field\FieldTypeCode
public function preSave() {
$link = $this
->get('link')
->getValue();
$social = $this
->get('social')
->getValue();
// Do not save if network or link are empty (just works in real entity field
// save process, not in field config form).
if (empty($link) && !empty($social)) {
$this
->set('social', '');
}
if (empty($social) && !empty($link)) {
$this
->set('social', '');
}
parent::preSave();
}