getParameter('person')->id(); $markup = ""; $person = \Drupal::entityTypeManager()->getStorage('person')->load($person_id); // If the person has no contact details, suggest they create some $link_to_add = "/zencrm/contact_details/$person_id/add?destination=/zencrm/person/$person_id"; $contact_details = \Drupal::entityTypeManager() ->getStorage('contact_details') ->loadByProperties(['person' => $person_id]); if (!reset($contact_details)) { $markup .= "

This person has no contact details yet. To get started, "; $markup .= "Add a set of contact details"; $markup .= "

"; } else { // They have contact details, so they are able to create hats. // If the person has no hats, suggest they create one, by rendering the hat creator block $link_to_add = "/zencrm/hat/$person_id/add?destination=/zencrm/person/$person_id"; $hats = \Drupal::entityTypeManager() ->getStorage('hat') ->loadByProperties(['person' => $person_id]); if (!reset($hats)) { $markup .= "

This person has no hats yet. A hat is a role that the person plays in the organisation. To get started, add a hat for this person.

"; $plugin_manager = \Drupal::service('plugin.manager.block'); $block = $plugin_manager->createInstance('hat_creator', array()); $markup .= render($block->build()); } else { // they have hats, so display the case view for each hat. foreach($hats as $hat) { $markup .= $this->showCases($hat); $markup .= $this->showCaseCreationLinks($hat); } } } return [ '#cache' => [ 'max-age' => 0, ], '#markup' => "
$markup
" ]; } // Renders a view showing cases that hat is involved in. private function showCases($hat) { $markup = ''; $markup .= "

" . $hat->name->getString() . "

"; $markup .= drupal_render(views_embed_view('this_hat_s_cases', 'block_1', $hat->id())); return "
$markup
"; } // Provides links to create different types of cases, passing in the hat id. private function showCaseCreationLinks($hat) { $hat_id = $hat->id(); $case_types = \Drupal::service('entity_type.bundle.info')->getBundleInfo('case_entity'); foreach($case_types as $case_type_id => $type) { $label = $type['label']; $markup .= "

Add a $label Case

"; } return ""; } }