Added custom block for creating profiles against a person.

It renders a link for each available profile type and they open in a popup which has a route and a controller.
This commit is contained in:
naomi 2018-04-10 17:19:03 +02:00
parent f764ed5670
commit 0e0b31cfcd
3 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,34 @@
<?php
namespace Drupal\zencrm\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
* Class ProfileController.
*/
class ProfileController extends ControllerBase {
/**
* Hello.
*
* @return string
* Return Hello string.
*/
public function createProfileForPerson($type, $person_id) {
$values = array(
'type' => $type,
'person' => $person_id
);
$node = \Drupal::entityTypeManager()
->getStorage('profile')
->create($values);
$form = \Drupal::entityTypeManager()
->getFormObject('profile', 'default')
->setEntity($node);
return \Drupal::formBuilder()->getForm($form);
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace Drupal\zencrm\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'ProfileCreator' block.
*
* @Block(
* id = "profile_creator",
* admin_label = @Translation("Profile creator"),
* )
*/
class ProfileCreator extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$person_id = \Drupal::routeMatch()->getParameter('person')->id();
$markup = "";
// Only offer profile creation on profiles they don't already have.
$profile_types = \Drupal::service('entity_type.bundle.info')->getBundleInfo('profile');
foreach($profile_types as $id => $type) {
$profiles = \Drupal::entityTypeManager()
->getStorage('profile')
->loadByProperties(['type' => $id, 'person' => $person_id]);
if (!reset($profiles)) {
$label = $type['label'];
$markup .= "<p><a class='use-ajax' data-dialog-type='modal' href='/zencrm/profile/add/$id/$person_id'>Create $label Profile</a></p>";
}
}
return [
'#cache' => [
'max-age' => 0,
],
'#markup' => "<div>$markup</div>"
];
}
}

7
zencrm.routing.yml Normal file
View File

@ -0,0 +1,7 @@
zencrm.profile.create:
path: '/zencrm/profile/add/{type}/{person_id}'
defaults:
_controller: '\Drupal\zencrm\Controller\ProfileController::createProfileForPerson'
_title: 'Add New Profile'
requirements:
_permission: 'access content'