/**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     $this->installConfig(SupportTicketTokenReplaceTest::$modules);
     $this->installEntitySchema('comment');
     $this->installEntitySchema('support_ticket');
     $support_ticket_type = entity_create('support_ticket_type', array('type' => 'test', 'name' => 'Test'));
     $support_ticket_type->save();
     support_ticket_add_body_field($support_ticket_type);
 }
 /**
  * Creates a custom ticket type based on default settings.
  *
  * @param array $values
  *   An array of settings to change from the defaults.
  *   Example: 'type' => 'foo'.
  *
  * @return \Drupal\support_ticket\Entity\SupportTicketType
  *   Created support ticket type.
  */
 protected function supportTicketCreateSupportTicketType(array $values = array())
 {
     // Find a non-existent random type name.
     if (!isset($values['type'])) {
         do {
             $id = strtolower($this->randomMachineName(8));
         } while (SupportTicketType::load($id));
     } else {
         $id = $values['type'];
     }
     $values += array('type' => $id, 'name' => $id);
     $type = entity_create('support_ticket_type', $values);
     $status = $type->save();
     support_ticket_add_body_field($type);
     \Drupal::service('router.builder')->rebuild();
     $this->assertEqual($status, SAVED_NEW, SafeMarkup::format('Created support ticket type %type.', array('%type' => $type->id())));
     return $type;
 }
 /**
  * {@inheritdoc}
  */
 public function save(array $form, FormStateInterface $form_state)
 {
     $type = $this->entity;
     $type->setNewRevision($form_state->getValue(array('options', 'revision')));
     $type->set('type', trim($type->id()));
     $type->set('name', trim($type->label()));
     $status = $type->save();
     $t_args = array('%name' => $type->label());
     if ($status == SAVED_UPDATED) {
         drupal_set_message(t('The support ticket type %name has been updated.', $t_args));
     } elseif ($status == SAVED_NEW) {
         support_ticket_add_body_field($type);
         // @todo
         drupal_set_message(t('The support ticket type %name has been added.', $t_args));
         $context = array_merge($t_args, array('link' => $type->link($this->t('View'), 'collection')));
         $this->logger('support_ticket')->notice('Added support ticket type %name.', $context);
     }
     $fields = $this->entityManager->getFieldDefinitions('support_ticket', $type->id());
     // Update title field definition.
     $title_field = $fields['title'];
     $title_label = $form_state->getValue('title_label');
     if ($title_field->getLabel() != $title_label) {
         $title_field->getConfig($type->id())->setLabel($title_label)->save();
     }
     // Update workflow options.
     $support_ticket = $this->entityManager->getStorage('support_ticket')->create(array('support_ticket_type' => $type->id()));
     foreach (array('status', 'locked') as $field_name) {
         $value = (bool) $form_state->getValue(['options', $field_name]);
         if ($support_ticket->{$field_name}->value != $value) {
             $fields[$field_name]->getConfig($type->id())->setDefaultValue($value)->save();
         }
     }
     $this->entityManager->clearCachedFieldDefinitions();
     $form_state->setRedirectUrl($type->urlInfo('collection'));
 }