diff --git a/modules/crm_tools/decoupled_auth_crm_tools.info.yml b/modules/crm_tools/decoupled_auth_crm_tools.info.yml new file mode 100644 index 0000000..5d032b3 --- /dev/null +++ b/modules/crm_tools/decoupled_auth_crm_tools.info.yml @@ -0,0 +1,8 @@ +name: Simple CRM Tools +description: "Tools for building CRM modules." +core: 8.x +type: module +package: Tool + +dependencies: + - decoupled_auth diff --git a/modules/crm_tools/decoupled_auth_crm_tools.module b/modules/crm_tools/decoupled_auth_crm_tools.module new file mode 100644 index 0000000..c1e4b27 --- /dev/null +++ b/modules/crm_tools/decoupled_auth_crm_tools.module @@ -0,0 +1,40 @@ +getFieldDefinitions() as $field_name => $definition) { + /* @var \Drupal\Core\Field\FieldDefinitionInterface $definition */ + if ($definition->getType() == 'status_log') { + $source_field = $definition->getFieldStorageDefinition()->getSetting('source_field'); + + $value = $entity->{$source_field}->value; + $original_value = $entity->original->{$source_field}->value; + if ($value !== $original_value) { + $values = [ + 'value' => $value, + 'previous' => $original_value, + 'uid' => \Drupal::currentUser()->id(), + 'timestamp' => \Drupal::time()->getRequestTime(), + ]; + $entity->{$field_name}->appendItem($values); + } + } + } +} \ No newline at end of file diff --git a/modules/crm_tools/src/Plugin/Field/FieldFormatter/StatusLogFormatter.php b/modules/crm_tools/src/Plugin/Field/FieldFormatter/StatusLogFormatter.php new file mode 100644 index 0000000..725ae53 --- /dev/null +++ b/modules/crm_tools/src/Plugin/Field/FieldFormatter/StatusLogFormatter.php @@ -0,0 +1,77 @@ + $item) { + /* @var \Drupal\decoupled_auth_crm_tools\Plugin\Field\FieldType\StatusLog $item */ + $values = $item->getValue(); + if (is_array($values) && !empty($values['value'])) { + + $elements[$delta]['value'] = [ + '#type' => 'html_tag', + '#tag' => 'p', + '#attributes' => ['class' => []], + '#value' => $this->t('@time: Status was changed from %status_old to %status_new by @username.', [ + '%status_new' => $values['value'], + '%status_old' => $values['previous'], + '@time' => DateTimePlus::createFromTimestamp($values['timestamp'])->format('Y-m-d H:i:s'), + '@username' => User::load($values['uid'])->label(), + ]), + ]; + + } + } + + return $elements; + } + +} diff --git a/modules/crm_tools/src/Plugin/Field/FieldType/StatusLog.php b/modules/crm_tools/src/Plugin/Field/FieldType/StatusLog.php new file mode 100644 index 0000000..87a385e --- /dev/null +++ b/modules/crm_tools/src/Plugin/Field/FieldType/StatusLog.php @@ -0,0 +1,168 @@ + 255, + ] + parent::defaultStorageSettings(); + } + + /** + * {@inheritdoc} + */ + public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { + // Prevent early t() calls by using the TranslatableMarkup. + $properties['value'] = DataDefinition::create('string') + ->setLabel(new TranslatableMarkup('Status')) + ->setRequired(TRUE); + + $properties['previous'] = DataDefinition::create('string') + ->setLabel(new TranslatableMarkup('Previous Status')); + + $properties['timestamp'] = DataDefinition::create('timestamp') + ->setLabel(new TranslatableMarkup('Changed timestamp')) + ->setRequired(TRUE); + + $properties['uid'] = DataDefinition::create('integer') + ->setLabel(new TranslatableMarkup('User responsible for change')); + + return $properties; + } + + /** + * {@inheritdoc} + */ + public static function schema(FieldStorageDefinitionInterface $field_definition) { + return [ + 'columns' => [ + 'value' => [ + 'type' => 'varchar', + 'length' => (int) $field_definition->getSetting('max_length'), + ], + 'previous' => [ + 'type' => 'varchar', + 'length' => (int) $field_definition->getSetting('max_length'), + ], + 'timestamp' => [ + 'type' => 'int', + ], + 'uid' => [ + 'type' => 'int', + ], + ], + 'indexes' => [ + 'value' => ['value'], + 'previous' => ['previous'], + ], + ]; + } + + /** + * {@inheritdoc} + */ + public function getConstraints() { + $constraints = parent::getConstraints(); + + if ($max_length = $this->getSetting('max_length')) { + $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager(); + $constraints[] = $constraint_manager->create('ComplexData', [ + 'value' => [ + 'Length' => [ + 'max' => $max_length, + 'maxMessage' => t('%name: may not be longer than @max characters.', [ + '%name' => $this->getFieldDefinition()->getLabel(), + '@max' => $max_length + ]), + ], + ], + ]); + } + + return $constraints; + } + + /** + * {@inheritdoc} + */ + public function applyDefaultValue($notify = TRUE) { + parent::applyDefaultValue($notify); + return $this; + } + + /** + * {@inheritdoc} + */ + public static function generateSampleValue(FieldDefinitionInterface $field_definition) { + $random = new Random(); + + $values['value'] = $random->word(mt_rand(1, $field_definition->getSetting('max_length'))); + $values['previous'] = $random->word(mt_rand(1, $field_definition->getSetting('max_length'))); + $values['uid'] = mt_rand(0, 1); + $values['timestamp'] = mt_rand(1262055681, 1262055681); + + return $values; + } + + /** + * {@inheritdoc} + */ + public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) { + $elements = []; + + $elements['max_length'] = [ + '#type' => 'number', + '#title' => t('Maximum length'), + '#default_value' => $this->getSetting('max_length'), + '#required' => TRUE, + '#description' => t('The maximum length of the field in characters.'), + '#min' => 1, + '#disabled' => $has_data, + ]; + + // @todo Make this field a select field of existing fields on entity. + $elements['source_field'] = [ + '#type' => 'textfield', + '#title' => t('Source Field'), + '#default_value' => $this->getSetting('source_field'), + '#required' => TRUE, + '#description' => t('The field that stores the status being logged.'), + '#disabled' => $has_data, + ]; + + return $elements; + } + + /** + * {@inheritdoc} + */ + public function isEmpty() { + $value = $this->get('value')->getValue(); + return $value === NULL || $value === ''; + } + +}