0

If I have an object (e.g. student object - this is my own custom class)

I would like to initiate this object during 'student registration' form initialization (or form build) and keep the object until I finish with the form (form submitted).

Is there any placeholder for me to hold this object in the FormBase class? Where I should declare my class? And how to keep the object?

Example in my code:

<?PHP

namespace bla;

use bla
use bla
use bla

class myCreateForm extends FormBase {

  private $student; // i want to initiate this (new student()) and keep until form submit
  /* where i should declare this? and how to keep it? */

  /**
   * Class constructor.
   */
  public function __construct($param) {
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'id_form';
  }

  public function buildForm(array $form, FormStateInterface $form_state) {
    //a lot of code here code here
    return $form;
  }

  public function validateForm(array &$form, FormStateInterface $form_state) {
    //a lot of code here code here
  }

  public function submitForm(array &$form, FormStateInterface $form_state) {
    $output = $this->student->save(); // at the end, i would like to have my own save process which my class will handle it.
    drupal_set_message($output);
  }

}
2
  • The first, you need understand DI. After you can create your service and load service to in construct and create.
    – MrD
    Aug 21, 2016 at 8:24
  • thank you for the reply, so you're suggesting use service instead - will it keep the same object value, e.g. in buildForm i set $student->setName('hello'); can i retrieve hello as a name in submitForm method?
    – AnD
    Aug 21, 2016 at 8:32

1 Answer 1

2

No, if this is a value object then a service not the answer.

I would recommend you look at entities. Drupal will do a ton of work for you if you use (content) entities for your value objects.

See https://www.drupal.org/node/2192175 for example for documentation, also check out the entity module for additional functionality that is not yet in Drupal core.

With content entities, there is a standard EntityForm class that contains an entity and allows you to save it.

That said, what you are doing basically works, $this->student will be available, but be aware that on the first form submission, formBuild() is called again, so it will build another object. The same for entities. If you really want to prevent that, you need to store your object in the database, e.g. with the tempstore API.

4
  • Thank you for the answer, do you mind to elaborate more on your concern why not use Service? because i just done using service and it work. i just wondering if there something that i should aware. thank you
    – AnD
    Aug 21, 2016 at 13:46
  • 1
    Services are API's. They should be stateless. So you probably want to have a student storage service, to load/save your students, but a student shouldn't be a service. That's just a data/value object. They're also not persisted across requests.
    – Berdir
    Aug 21, 2016 at 14:10
  • I think entity is good and standard of Drupal. But i think entity is CRUD. We just use entity when one class need full CRUD. However, in some case, we can use service is better. Example is module Ban in core Drupal. Just in my mind.
    – MrD
    Aug 21, 2016 at 15:16
  • 1
    True, but having a form with an attached class sounds like full CRUD to me. Also, ban.module does exactly what I said: A storage/manager service to check/store ban entries. a single ban entry is not a service.
    – Berdir
    Aug 21, 2016 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.