Created
April 11, 2017 19:09
-
-
Save johnny5th/80e10841fa65a71acb0291228c27ef3a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\hosting_management\Form; | |
use Drupal\Core\Entity\ContentEntityForm; | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\message\Entity\Message; | |
/** | |
* Form controller for Customer edit forms. | |
* | |
* @ingroup hosting_management | |
*/ | |
class CustomerForm extends ContentEntityForm { | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildForm(array $form, FormStateInterface $form_state) { | |
/* @var $entity \Drupal\hosting_management\Entity\Customer */ | |
$form = parent::buildForm($form, $form_state); | |
// $noteform = \Drupal::formBuilder()->getForm('Drupal\hosting_management\Form\CustomerNoteForm'); | |
// unset($noteform['#theme_wrappers']); | |
$noteform = array(); | |
$noteform['#prefix'] = '<div id="note-form">'; | |
$noteform['#suffix'] = '</div>'; | |
$noteform['note'] = array( | |
'#type' => 'textarea', | |
'#title' => $this->t('Note'), | |
'#weight' => 1, | |
); | |
$noteform['actions']['#type'] = 'actions'; | |
$noteform['actions']['#weight'] = 2; | |
$noteform['actions']['submit'] = array( | |
'#type' => 'submit', | |
'#value' => $this->t('Add Note'), | |
'#name' => 'note-submit', | |
'#button_type' => 'primary', | |
'#callback' => '::customer_note_form_submit', | |
'#ajax' => array( | |
'callback' => array($this, 'customer_note_form_js_submit'), | |
'wrapper' => 'note-form', | |
'method' => 'replace', | |
'effect' => 'fade', | |
), | |
); | |
$noteform['customer'] = array('#type' => 'hidden', '#value' => $form_state->getFormObject()->getEntity()->id()); | |
$noteform['note_list'] = views_embed_view('customer_messages', 'default', $form_state->getFormObject()->getEntity()->id()); | |
$noteform['note_list']['#weight'] = 3; | |
$form['note_form'] = $noteform; | |
$entity = $this->entity; | |
return $form; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function save(array $form, FormStateInterface $form_state) { | |
$entity = &$this->entity; | |
$status = parent::save($form, $form_state); | |
switch ($status) { | |
case SAVED_NEW: | |
drupal_set_message($this->t('Created the %label Customer.', [ | |
'%label' => $entity->label(), | |
])); | |
break; | |
default: | |
drupal_set_message($this->t('Saved the %label Customer.', [ | |
'%label' => $entity->label(), | |
])); | |
} | |
$form_state->setRedirect('entity.customer.canonical', ['customer' => $entity->id()]); | |
} | |
public function customer_note_form_js_submit($form, $form_state) { | |
$message = Message::create(['template' => 'customer_note', 'uid' => \Drupal::currentUser()->id()]); | |
$customer_id = 1; | |
$message->set('field_customer_ref', $form_state->getValue('customer')); | |
$message->set('field_note', $form_state->getValue('note')); | |
$message->save(); | |
return $form['note_form']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment