EditOwnContactInformationBlock.php in CRM Core 8
File
modules/crm_core_user_sync/src/Plugin/Block/EditOwnContactInformationBlock.php
View source
<?php
namespace Drupal\crm_core_user_sync\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\crm_core_contact\Entity\Individual;
use Drupal\crm_core_user_sync\CrmCoreUserSyncRelationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EditOwnContactInformationBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $relation;
protected $entityTypeManager;
protected $formBuilder;
protected $accountProxy;
public function __construct(array $configuration, $plugin_id, $plugin_definition, CrmCoreUserSyncRelationInterface $relation, EntityTypeManagerInterface $entityTypeManager, FormBuilderInterface $formBuilder, AccountProxyInterface $accountProxy) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->relation = $relation;
$this->entityTypeManager = $entityTypeManager;
$this->formBuilder = $formBuilder;
$this->accountProxy = $accountProxy;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('crm_core_user_sync.relation'), $container
->get('entity_type.manager'), $container
->get('form_builder'), $container
->get('current_user'));
}
protected function blockAccess(AccountInterface $account) {
return AccessResult::allowedIfHasPermission($account, 'edit own contact information');
}
public function build() {
if ($individualId = $this->relation
->getUserIndividualId($this->accountProxy
->id())) {
$individual = Individual::load($individualId);
$form = $this->entityTypeManager
->getFormObject($individual
->getEntityTypeId(), 'default');
$form
->setEntity($individual);
$form_state = new FormState();
$form_state
->disableRedirect();
$build = $this->formBuilder
->buildForm($form, $form_state);
unset($build['actions']['delete']);
return $build;
}
}
}