summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKirill Roskolii2018-08-30 21:15:41 (GMT)
committerKirill Roskolii2018-08-30 21:15:41 (GMT)
commit1bea17f308ddcfd7f808aba216dc5ca09641f989 (patch)
treeb76982983bb25ba4d3a322ef1cc22608aaa80d73
parent5fd91857107426e5aaf6192fbc1ea6f5c168eea3 (diff)
Added ability to edit own contact information.
-rw-r--r--modules/crm_core_user_sync/src/Plugin/Block/EditOwnContactInformationBlock.php117
1 files changed, 117 insertions, 0 deletions
diff --git a/modules/crm_core_user_sync/src/Plugin/Block/EditOwnContactInformationBlock.php b/modules/crm_core_user_sync/src/Plugin/Block/EditOwnContactInformationBlock.php
new file mode 100644
index 0000000..9f9af11
--- /dev/null
+++ b/modules/crm_core_user_sync/src/Plugin/Block/EditOwnContactInformationBlock.php
@@ -0,0 +1,117 @@
+<?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\crm_core_contact\Entity\Individual;
+use Drupal\crm_core_user_sync\CrmCoreUserSyncRelationInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/**
+ * Provides a 'Edit own contact information' block.
+ *
+ * @Block(
+ * id = "crm_core_user_sync_edit_own_contact_information",
+ * admin_label = @Translation("Edit own contact information"),
+ * category = @Translation("CRM Core")
+ * )
+ */
+class EditOwnContactInformationBlock extends BlockBase implements ContainerFactoryPluginInterface {
+
+ /**
+ * Relation service.
+ *
+ * @var \Drupal\crm_core_user_sync\CrmCoreUserSyncRelationInterface
+ */
+ protected $relation;
+
+ /**
+ * Entity type manager.
+ *
+ * @var \Drupal\Core\Entity\EntityTypeManagerInterface
+ */
+ protected $entityTypeManager;
+
+ /**
+ * Form builder.
+ *
+ * @var \Drupal\Core\Form\FormBuilderInterface
+ */
+ protected $formBuilder;
+
+ /**
+ * Constructs a new EditOwnContactInformationBlock instance.
+ *
+ * @param array $configuration
+ * The plugin configuration, i.e. an array with configuration values keyed
+ * by configuration option name. The special key 'context' may be used to
+ * initialize the defined contexts by setting it to an array of context
+ * values keyed by context names.
+ * @param string $plugin_id
+ * The plugin_id for the plugin instance.
+ * @param mixed $plugin_definition
+ * The plugin implementation definition.
+ * @param \Drupal\crm_core_user_sync\CrmCoreUserSyncRelationInterface $relation
+ * Relation service.
+ * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
+ * Entity type manager.
+ * @param \Drupal\Core\Form\FormBuilderInterface $formBuilder
+ * Form builder.
+ */
+ public function __construct(array $configuration, $plugin_id, $plugin_definition, CrmCoreUserSyncRelationInterface $relation, EntityTypeManagerInterface $entityTypeManager, FormBuilderInterface $formBuilder) {
+ parent::__construct($configuration, $plugin_id, $plugin_definition);
+ $this->relation = $relation;
+ $this->entityTypeManager = $entityTypeManager;
+ $this->formBuilder = $formBuilder;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ 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')
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function blockAccess(AccountInterface $account) {
+ return AccessResult::allowedIfHasPermission($account, 'edit own contact information');
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function build() {
+
+ if ($individualId = $this->relation->getUserIndividualId(\Drupal::currentUser()->id())) {
+ /* @var $individual \Drupal\crm_core_contact\Entity\Individual */
+ /* @var $form \Drupal\crm_core_contact\Form\IndividualForm */
+ $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;
+ }
+ }
+
+}