0

I have a web service on third party system that return property of doctor and I have a content type named "doctor". I want when user go to a url like doctor/23 drupal create a node of type doctor based on url arg and show this node to user. for that and with @zaporylie help i create a custom controller like bellow

create a Routing:

    showdoctor.load_doctor_controller_load:
  path: 'doctor/{doctor_id}'
  defaults:
    _controller: '\Drupal\showdoctor\Controller\LoadDoctorController::load'
    _title: 'load'
  requirements:
    _permission: 'access content'

and my controller like this:

    class LoadDoctorController extends ControllerBase
{
    public function load($doctor_id)
    {
        $result = RestCurl::post("http://myrest_server.com", [
            'filter' => ['doctor.id' => $doctor_id],
        ]);

        $fullname = $result['data']->message->data[0]->firstName;
        $fullname = $fullname . ' ' . $result['data']->message->data[0]->lastName;
        // Fetch doctor node with EFQ
        $query = \Drupal::entityQuery('node');
        $query->condition('type', 'doctor');
        $query->condition('field_doctor_id', $doctor_id);
        $nids = $query->execute();
        if (!$nids) {
            $node = Node::create([
                'type' => 'doctor',
                'title' => $fullname,
                'uid' => 1,
            ]);
            $node->set('field_doctor_id', $doctor_id);
            $node->save();
            $element = array(
                '#markup' => $node,
            );
            return $element;
        } else {
            $element = array(
                '#markup' => $node,
            );
            return $element;
        }
    }

}

Now for example when in go to doctor/23 page i see a page with load title. and if i refresh this page i see doctor page correctly. i think my problem is return value.

1 Answer 1

2

You should create a custom Controller rather than hook on page load. This way you won't have an overhead on every page load. In the Controller (accessible under movie/{movie_id} route) try to load movie by movie_id (entityQuery), and if it does not exists simply create one based on data from 3rd party service and display in a display mode of your choice.

1
  • Thanks @zaporylie. in Controller how return value for show created movie?
    – Arefen
    Jul 23, 2018 at 10:49

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.