View source
<?php
namespace Drupal\apigee_api_catalog\Entity\Form;
use Drupal\apigee_api_catalog\Entity\ApiDocInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
class ApiDocForm extends ContentEntityForm {
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$form['specifications'] = [
'#type' => 'fieldset',
'#title' => $this
->t('OpenAPI Specifications File'),
'#weight' => $form['spec_file_source']['#weight'],
];
$form['spec']['#states'] = [
'visible' => [
':input[name="spec_file_source"]' => [
'value' => ApiDocInterface::SPEC_AS_FILE,
],
],
'required' => [
':input[name="spec_file_source"]' => [
'value' => ApiDocInterface::SPEC_AS_FILE,
],
],
];
$form['file_link']['#states'] = [
'visible' => [
':input[name="spec_file_source"]' => [
'value' => ApiDocInterface::SPEC_AS_URL,
],
],
'required' => [
':input[name="spec_file_source"]' => [
'value' => ApiDocInterface::SPEC_AS_URL,
],
],
];
$form['specifications']['spec_file_source'] = $form['spec_file_source'];
$form['specifications']['spec'] = $form['spec'];
$form['specifications']['file_link'] = $form['file_link'];
unset($form['spec_file_source'], $form['spec'], $form['file_link']);
return $form;
}
protected function getNewRevisionDefault() {
$entity = $this
->getEntity();
$new_revision_default = $entity
->shouldCreateNewRevision();
return $new_revision_default;
}
public function save(array $form, FormStateInterface $form_state) {
$entity = $this
->getEntity();
$insert = $entity
->isNew();
parent::save($form, $form_state);
$singular_label = $this->entity
->getEntityType()
->getSingularLabel();
if ($insert) {
$this
->messenger()
->addMessage($this
->t('Created the %label @entity_type_label.', [
'%label' => $entity
->label(),
'@entity_type_label' => $singular_label,
]));
}
else {
$this
->messenger()
->addMessage($this
->t('Saved the %label @entity_type_label.', [
'%label' => $entity
->label(),
'@entity_type_label' => $singular_label,
]));
}
$form_state
->setRedirect('entity.apidoc.collection');
}
}