0

I generated an Entity following this guide. Now I would like to create a custom form to modify only some fields inside a single entity. I was thinking to create an action inside the controller calling a new/custom form:

  1. I created the routing file to open the form with the specified path (I know I can also create an action inside the controller to open the form with a custom template)
  2. I created the form that extends the main EntityForm:

    class DetailedForm extends CompleteEntityForm{
        public function getFormId() {
           return 'detailed_form';
        }
    
        public function buildForm(array $form, FormStateInterface $form_state){
            $form = parent::buildForm($form, $form_state);
            return $form:
        }
     }
    
  3. I put some lines of code inside the functions getFormId(), buildForm(), validateForm() and submitForm().

But in the end the result is an error:

Call to a member function getEntityTypeId() on null in \drupal\core\lib\Drupal\Core\Entity\EntityForm.php on line 79 #0 \drupal\core\lib\Drupal\Core\Form\FormBuilder.php(196): Drupal\Core\Entity\EntityForm->getBaseFormId()

Any suggestions?

8
  • This is a typical error when you call an entity form without providing an entity.
    – 4uk4
    Aug 7, 2018 at 15:02
  • @4k4 yep, but what can i do?
    – que le
    Aug 8, 2018 at 7:07
  • If my guess is right then the error message has nothing to do with the code you've shown so far. There might be something wrong with the route the entity provides or how the action calls the entity route.
    – 4uk4
    Aug 8, 2018 at 7:42
  • @4k4 it's correct. i found the problem. I'm going to update and close the issue. thank you
    – que le
    Aug 8, 2018 at 14:20
  • @4k4 i updated my question with a new issue :/
    – que le
    Aug 9, 2018 at 7:14

1 Answer 1

-1

Based on the comments on the question, I fixed my issue.

I created the routing path to call the controller:

mymodule.manage_detailed:
  path: '/mes-detailed/cp/{eid}'
  defaults:
    _controller: '\Drupal\mymodule\Controller\DetailedController::manageAction'
    _title: 'Detailed'
  requirements:
    _permission: 'access content'

Controller:

class DetailedController extends ControllerBase implements ContainerInjectionInterface {

public function manageAction($eid) { 

    $entityForm = \Drupal::entityTypeManager()
        ->getStorage('main_entity')
        ->load($eid);
    $finalform =  $this->entityFormBuilder()->getForm($entityForm);

    $render['form'] = $finalform;

    $render['#theme'] = 'manage_detailed';
    return $render;

}


}

Then I put inside the template all the form with:

{{ form }}

Because using only some fields inside the template didn't allow to save data. And finally I create the submit function to define the redirect:

public static function manage_form_submit(&$form, &$form_state) {
        $url = \Drupal\Core\Url::fromUri('internal:/link-to-go');
        $form_state->setRedirectUrl($url);
    }

To call the submit function I used the alter form function inside the module file:

function mymodule_form_alter(&$form, &$form_state, $form_id) {

 //drupal_set_message($form_id); //to show id of the form

 if ($form_id == 'name_of_entity_form'){

    $form['name']['#type'] = 'hidden';
    $form['status']['#type'] = 'hidden';
    $form['new_revision']['#type'] = 'hidden';
    $form['revision_log_message']['#type'] = 'hidden';
    $form['field_n1']['#type'] = 'hidden';
    $form['field_n2']['#type'] = 'hidden';
    $form['field_email_']['#type'] = 'hidden';
    $form['field_nd']['#type'] = 'hidden';
    $form['actions']['delete']['#type'] = 'hidden';

    $form['actions']['submit']['#submit'][] = 'Drupal\mymodule\Controller\DetailedController::manage_form_submit';

  }
}
0

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.