You are here

public function EntityLegalDocument::save in Entity Legal 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Entity/EntityLegalDocument.php \Drupal\entity_legal\Entity\EntityLegalDocument::save()
  2. 4.0.x src/Entity/EntityLegalDocument.php \Drupal\entity_legal\Entity\EntityLegalDocument::save()

Saves an entity permanently.

When saving existing entities, the entity is assumed to be complete, partial updates of entities are not supported.

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Throws

\Drupal\Core\Entity\EntityStorageException In case of failures an exception is thrown.

Overrides ConfigEntityBase::save

File

src/Entity/EntityLegalDocument.php, line 303

Class

EntityLegalDocument
Defines the entity legal document entity.

Namespace

Drupal\entity_legal\Entity

Code

public function save() {
  $status = parent::save();
  if ($status == SAVED_NEW && !\Drupal::isConfigSyncing()) {

    // Add or remove the body field, as needed.
    $field = FieldConfig::loadByName('entity_legal_document_version', $this
      ->id(), 'entity_legal_document_text');
    if (empty($field)) {
      FieldConfig::create([
        'field_storage' => FieldStorageConfig::loadByName('entity_legal_document_version', 'entity_legal_document_text'),
        'bundle' => $this
          ->id(),
        'label' => 'Document text',
        'settings' => [
          'display_summary' => FALSE,
        ],
      ])
        ->save();

      // Assign widget settings for the 'default' form mode.
      \Drupal::service('entity_display.repository')
        ->getFormDisplay('entity_legal_document_version', $this
        ->id(), 'default')
        ->setComponent('entity_legal_document_text', [
        'type' => 'text_textarea_with_summary',
      ])
        ->save();

      // Assign display settings for 'default' view mode.
      \Drupal::service('entity_display.repository')
        ->getViewDisplay('entity_legal_document_version', $this
        ->id(), 'default')
        ->setComponent('entity_legal_document_text', [
        'label' => 'hidden',
        'type' => 'text_default',
      ])
        ->save();
    }
  }
  else {
    Cache::invalidateTags([
      "entity_legal_document:{$this->id()}",
    ]);
  }
  return $status;
}