I have the following issue with my form class that extends the ContentEntityForm class.

When calling the parent buildForm which is needed my system runs out of memory.

   * {@inheritdoc}
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    // Here is already runs of memory. $form is never initiated.

    /* @var $entity \Drupal\sg_configuration_rule\Entity\ConfigurationRule */
    $entity = $this->entity;

    $form_state->set('old_cron_value', $entity->get('cron_settings')->first()->value);

    $type = FALSE;

    if (!$entity->isNew()) {
      $type = $entity->getPluginInstance()->getPluginId();

    if ($entity->isNew()) {
      $type = \Drupal::request()->query->get('type');
      if (!$type) {
        return new RedirectResponse(Url::fromRoute('configuration_rule.add_form_step1')->toString());

    if ($type) {
      try {
        /** @var \Drupal\sg_base_api\Plugin\BaseApiPluginInterface $enabled_api */
        $enabled_api = $this->baseApiPluginManager->createInstance($type);
      catch (PluginException $exception) {
        return new RedirectResponse(Url::fromRoute('configuration_rule.add_form_step1')->toString());

      $enabled_api->configRuleForm($form, $entity);

      $form['plugin_type']['widget'][0]['value']['#value'] = $type;
      $form['plugin_type']['widget'][0]['value']['#access'] = FALSE;
      $form['plugin_type']['widget'][0]['value']['#disabled'] = TRUE;
      $form['server_node']['widget']['#options'] = $this->getServerNodesByType($enabled_api->entityType());

    $form['user_id']['#access'] = FALSE;

    return $form;

When i check the parent function i noticed that the line:

$form = $this->form($form, $form_state); is causing this in the class EntityForm(Core method).

   * {@inheritdoc}
  public function buildForm(array $form, FormStateInterface $form_state) {
    // During the initial form build, add this form object to the form state and
    // allow for initial preparation before form building and processing.
    if (!$form_state->has('entity_form_initialized')) {

    // Ensure that edit forms have the correct cacheability metadata so they can
    // be cached.
    if (!$this->entity->isNew()) {
      \Drupal::service('renderer')->addCacheableDependency($form, $this->entity);

    // Retrieve the form array using the possibly updated entity in form state.
    // This is causing my memory timeout.
    $form = $this->form($form, $form_state);

    // Retrieve and add the form actions array.
    $actions = $this->actionsElement($form, $form_state);
    if (!empty($actions)) {
      $form['actions'] = $actions;

    return $form;

If i comment that line out it is working fine but this is needed to save my values in config. Also this is core and should work.

Anyone else have this problem and knows the solutions to this?

enter image description here


  • Can you try getting more detailed stack trace or perhaps even just use xdebug and debug yourself?
    – Beebee
    Commented Aug 29, 2019 at 10:20
  • What is memory_limit set to in php.ini?
    – user72672
    Commented Aug 29, 2019 at 13:08
  • The issue is probably a recursion caused by the entity for which the form is built.
    – 4uk4
    Commented Aug 29, 2019 at 13:55

1 Answer 1


Are you sure this is a coding error? Are you loading any large quantities of content in the form by any chance? Loading a lot of nodes in a select field, for instance, can generate memory overflows.

Your Answer

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

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