Added route and controller for adding activity to case

This commit is contained in:
naomi 2018-04-14 14:40:37 +02:00
parent 7eb0ee092b
commit 0dec481020
4 changed files with 85 additions and 2 deletions

View File

@ -182,14 +182,14 @@ class Activity extends ContentEntityBase implements ActivityInterface {
// This field is always implied from the context,
// so has no form or view display.
$fields['case'] = BaseFieldDefinition::create('entity_reference')
$fields['case_entity'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Case'))
->setDescription(t('The case this activity belongs to.'))
->setSetting('target_type', 'case_entity');
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of the Activity entity.'))
->setDescription(t('The name of the Activity.'))
->setSettings([
'max_length' => 50,
'text_processing' => 0,

View File

@ -0,0 +1,35 @@
<?php
namespace Drupal\zencrm\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
* Class ActivityController.
*/
class ActivityController extends ControllerBase {
/**
* Displays a form for creating a activity.
* The type of activity and the case are prepopulated.
*
* @return form for creating a activity
*/
public function createActivityForCase($case_id, $activity_type_id) {
error_log( "$case_id, $activity_type_id");
$values = array(
'type' => $activity_type_id,
'case_entity' => $case_id,
);
$activity = \Drupal::entityTypeManager()
->getStorage('activity')
->create($values);
$form = \Drupal::entityTypeManager()
->getFormObject('activity', 'default')
->setEntity($activity);
return \Drupal::formBuilder()->getForm($form);
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace Drupal\zencrm\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'ActivityCreator' block.
* Block contains links for creating activities with.
* The links open an entity create form in a popup, passing in the activity type and case id in the url.
*
* @Block(
* id = "activity_creator",
* admin_label = @Translation("Activity creator"),
* )
*/
class ActivityCreator extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$case_id = \Drupal::routeMatch()->getParameter('case_entity')->id();
$markup = "";
$activity_types = \Drupal::service('entity_type.bundle.info')->getBundleInfo('activity');
foreach($activity_types as $activity_type_id => $type) {
$label = $type['label'];
$markup .= "<p><a class='use-ajax' data-dialog-type='modal' href='/zencrm/activity/$case_id/add/$activity_type_id?destination=/zencrm/case/$case_id'>Add a $label Activity</a></p>";
}
return [
'#cache' => [
'max-age' => 0,
],
'#markup' => "<div class='zencrm_creation_links'>$markup</div>"
];
}
}

View File

@ -30,3 +30,11 @@ zencrm.case.create:
requirements:
_permission: 'access content'
zencrm.activity.create:
path: '/zencrm/activity/{case_id}/add/{activity_type_id}'
defaults:
_controller: '\Drupal\zencrm\Controller\ActivityController::createActivityForCase'
_title: 'Add New Activity'
requirements:
_permission: 'access content'