Skip to content

Commit

Permalink
refs #civicrm_fields added inline field widgets.
Browse files Browse the repository at this point in the history
  • Loading branch information
kewljuice committed Mar 14, 2019
1 parent 6d81eeb commit 29f81b2
Show file tree
Hide file tree
Showing 3 changed files with 249 additions and 0 deletions.
83 changes: 83 additions & 0 deletions src/Plugin/Field/FieldWidget/ContactInlineWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Drupal\civicrm_fields\Plugin\Field\FieldWidget;

use Drupal\civicrm_fields\Utility\CivicrmService;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Plugin implementation of the 'civicrm_field_contact' field widget.
*
* @FieldWidget(
* id = "civicrm_field_contact_inline_widget",
* label = @Translation("CiviCRM contact inline widget"),
* field_types = {
* "civicrm_field_contact",
* }
* )
*/
class ContactInlineWidget extends WidgetBase implements ContainerFactoryPluginInterface {

/**
* The CiviCRM API service.
*
* @var \Drupal\civicrm_fields\Utility\CivicrmService
*/
protected $service;

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['third_party_settings'],
$container->get('civicrm.service')
);
}

/**
* {@inheritdoc}
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, CivicrmService $service) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->service = $service;
}

/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$item =& $items[$delta];
// Store element(s) for the field.
$element = [];
// Fetch default value.
$default = NULL;
if (isset($item->contact_id)) {
$result = $this->service->api('Contact', 'GetSingle', ['contact_id' => $item->contact_id]);
$default = $result['display_name'] . ' (' . $result['contact_id'] . ')';
}
// Autocomplete field for contact_id.
$element['contact_id'] = [
'#title' => $this->t('CiviCRM Contact ID'),
'#type' => 'textfield',
'#autocomplete_route_name' => 'civicrm_fields.endpoint',
'#autocomplete_route_parameters' => [
'entity' => 'contact',
'count' => 10,
],
'#default_value' => $default,
];
// Return element(s).
return $element;
}

}
83 changes: 83 additions & 0 deletions src/Plugin/Field/FieldWidget/ContributionPageInlineWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Drupal\civicrm_fields\Plugin\Field\FieldWidget;

use Drupal\civicrm_fields\Utility\CivicrmService;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Plugin implementation of the 'civicrm_field_contribution_page' field widget.
*
* @FieldWidget(
* id = "civicrm_field_contribution_page_inline_widget",
* label = @Translation("CiviCRM contribution page inline widget"),
* field_types = {
* "civicrm_field_contribution_page",
* }
* )
*/
class ContributionPageInlineWidget extends WidgetBase implements ContainerFactoryPluginInterface {

/**
* The CiviCRM API service.
*
* @var \Drupal\civicrm_fields\Utility\CivicrmService
*/
protected $service;

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['third_party_settings'],
$container->get('civicrm.service')
);
}

/**
* {@inheritdoc}
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, CivicrmService $service) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->service = $service;
}

/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$item =& $items[$delta];
// Store element(s) for the field.
$element = [];
// Fetch default value.
$default = NULL;
if (isset($item->contribution_page_id)) {
$result = $this->service->api('ContributionPage', 'GetSingle', ['id' => $item->contribution_page_id]);
$default = $result['title'] . ' (' . $result['id'] . ')';
}
// Autocomplete field for contribution_page_id.
$element['contribution_page_id'] = [
'#title' => $this->t('CiviCRM Contribution ID'),
'#type' => 'textfield',
'#autocomplete_route_name' => 'civicrm_fields.endpoint',
'#autocomplete_route_parameters' => [
'entity' => 'contributionPage',
'count' => 10,
],
'#default_value' => $default,
];
// Return element(s).
return $element;
}

}
83 changes: 83 additions & 0 deletions src/Plugin/Field/FieldWidget/EventInlineWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Drupal\civicrm_fields\Plugin\Field\FieldWidget;

use Drupal\civicrm_fields\Utility\CivicrmService;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Plugin implementation of the 'civicrm_field_event' field widget.
*
* @FieldWidget(
* id = "civicrm_field_event_inline_widget",
* label = @Translation("CiviCRM event inline widget"),
* field_types = {
* "civicrm_field_event",
* }
* )
*/
class EventInlineWidget extends WidgetBase implements ContainerFactoryPluginInterface {

/**
* The CiviCRM API service.
*
* @var \Drupal\civicrm_fields\Utility\CivicrmService
*/
protected $service;

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['third_party_settings'],
$container->get('civicrm.service')
);
}

/**
* {@inheritdoc}
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, CivicrmService $service) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);
$this->service = $service;
}

/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$item =& $items[$delta];
// Store element(s) for the field.
$element = [];
// Fetch default value.
$default = NULL;
if (isset($item->event_id)) {
$result = $this->service->api('Event', 'GetSingle', ['id' => $item->event_id]);
$default = $result['title'] . ' (' . $result['id'] . ')';
}
// Autocomplete field for event_id.
$element['event_id'] = [
'#title' => $this->t('CiviCRM Event ID'),
'#type' => 'textfield',
'#autocomplete_route_name' => 'civicrm_fields.endpoint',
'#autocomplete_route_parameters' => [
'entity' => 'event',
'count' => 10,
],
'#default_value' => $default,
];
// Return element(s).
return $element;
}

}

0 comments on commit 29f81b2

Please sign in to comment.