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.