Skip to content

Instantly share code, notes, and snippets.

@rachellawson
Created February 20, 2017 11:44
Show Gist options
  • Save rachellawson/c4a2143ac82e9713ddde719ca00b6137 to your computer and use it in GitHub Desktop.
Save rachellawson/c4a2143ac82e9713ddde719ca00b6137 to your computer and use it in GitHub Desktop.
How I am checking to make sure I use the same bundle type on a poi_experience_entity as that of the poi_entity it will be referencing
<?php
namespace Drupal\destinations\Form;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Form controller for Point of Interest Experience Entity edit forms.
*
* @ingroup destinations
*/
class POIExperienceEntityForm extends ContentEntityForm {
/**
* {@inheritdoc}
*/
public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id) {
$poi_entity = \Drupal::entityTypeManager()
->getStorage('poi_entity')
->load($route_match->getParameter('poi_entity'));
if ($route_match->getRawParameter($entity_type_id) !== NULL) {
$poi_experience_entity = $route_match->getParameter($entity_type_id);
}
else {
$entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
$bundle_key = $entity_type->getKey('bundle');
$values[$bundle_key] = $poi_entity->bundle();
//@TODO add in the id of the poi_entity.
$poi_experience_entity = $this->entityTypeManager->getStorage($entity_type_id)->create($values);
}
return $poi_experience_entity;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
/* @var $entity \Drupal\destinations\Entity\POIExperienceEntity */
$form = parent::buildForm($form, $form_state);
if (!$this->entity->isNew()) {
$form['new_revision'] = array(
'#type' => 'hidden',
'#title' => $this->t('Create new revision'),
'#default_value' => TRUE,
'#weight' => 10,
);
} else {
// We don't need to record a revision message on new Destinations.
$form['revision_log_message']['#type'] = 'hidden';
}
// We never need to manually alter the url alias.
$form['path']['#type'] = 'hidden';
$entity = $this->entity;
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$entity = &$this->entity;
// Save as a new revision if requested to do so.
if (!$form_state->isValueEmpty('new_revision') && $form_state->getValue('new_revision') != FALSE) {
$entity->setNewRevision();
// If a new revision is created, save the current user as revision author.
$entity->setRevisionCreationTime(REQUEST_TIME);
$entity->setRevisionUserId(\Drupal::currentUser()->id());
}
else {
$entity->setNewRevision(FALSE);
}
$status = parent::save($form, $form_state);
switch ($status) {
case SAVED_NEW:
drupal_set_message($this->t('Created the %label Point of Interest Experience.', [
'%label' => $entity->label(),
]));
break;
default:
drupal_set_message($this->t('Saved the %label Point of Interest Experience.', [
'%label' => $entity->label(),
]));
}
$form_state->setRedirect('entity.poi_experience_entity.canonical', ['poi_experience_entity' => $entity->id()]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment