marketo_ma-8.x-2.x-dev/marketo_ma.install
marketo_ma.install
<?php
/**
* @file
* Install hooks for Marketo MA module.
*/
use Drupal\Core\Database\Database;
use Drupal\Core\Url;
use Drupal\marketo_ma\Service\MarketoMaServiceInterface;
use NecLimDul\MarketoRest\Lead\Api\LeadsApi;
/**
* Implements hook_schema().
*/
function marketo_ma_schema() {
$schema['marketo_ma_lead_fields'] = [
'description' => 'Marketo Lead Field Detail.',
'primary key' => ['id'],
'unique keys' => [
'restName_unique_key' => ['restName'],
],
'fields' => [
'id' => [
'description' => 'Internal identifier for field.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
'displayName' => [
'description' => 'Lead field display name.',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
],
'dataType' => [
'description' => 'Lead field data type.',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
],
'length' => [
'description' => 'Lead field length.',
'type' => 'int',
'unsigned' => TRUE,
],
'restName' => [
'description' => 'Lead field key for REST API.',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
],
'restReadOnly' => [
'description' => 'TRUE (1) if REST field is read only.',
'type' => 'int',
'size' => 'small',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'soapName' => [
'description' => 'Lead field key for SOAP API.',
'type' => 'varchar',
'length' => 128,
],
'soapReadOnly' => [
'description' => 'TRUE (1) if SOAP field is read only.',
'type' => 'int',
'size' => 'small',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
],
];
return $schema;
}
/**
* Implements hook_requirements().
*/
function marketo_ma_requirements($phase) {
if ($phase == 'runtime') {
// Run Marketo REST tests.
/**
* @var \Drupal\marketo_ma\Service\MarketoMaApiClientInterface $marketo_client
*/
$marketo_client = \Drupal::service('marketo_ma.api_client');
if (!class_exists(LeadsApi::class)) {
// The composer library is missing.
return [
'marketo_ma_rest_api_client' => [
'description' => t("The Marketo REST api client is required
for the Marketo MA module. Please read the module's readme for
installation instructions."),
'title' => t('Marketo REST client'),
'severity' => REQUIREMENT_ERROR,
'value' => t('Not found'),
],
];
}
elseif (!$marketo_client->canConnect()) {
// Some required configuration is missing for the Marketo API.
return [
'marketo_ma_rest_configuration' => [
'description' => t('The Marketo REST API was chosen for connection method but some <a href=":link">configuration</a> seems to be missing.', [
':link' => Url::fromRoute('marketo_ma.settings')->toString(),
]),
'title' => t('Marketo MA configuration.'),
'severity' => REQUIREMENT_ERROR,
'value' => t('Configuration incomplete.'),
],
];
}
else {
// Get available lead fields from marketo.
// @todo Consider moving this check to the configuration form.
try {
$marketo_client->getFields();
}
catch (\Exception $e) {
return [
'marketo_ma_rest_connection' => [
'description' => t('There was an error connecting to the Marketo REST API. check your api credentials on the <a href=":link">configuration</a> page: %message', [
'%message' => $e->getMessage(),
':link' => Url::fromRoute('marketo_ma.settings')->toString(),
]),
'title' => t('Marketo REST Connection'),
'severity' => REQUIREMENT_ERROR,
'value' => t('Connection Error'),
],
];
}
// Unable to get a valid response from Marketo for some reason.
return [
'marketo_ma_rest_connection' => [
'description' => t('Everything seems to be working as expected.', [
':link' => Url::fromRoute('marketo_ma.settings')->toString(),
]),
'title' => t('Marketo REST Connection'),
'severity' => REQUIREMENT_OK,
'value' => t('Connection Successful'),
],
];
}
}
return [];
}
/**
* Remove unused lead_source config.
*/
function marketo_ma_update_8300() {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable(MarketoMaServiceInterface::MARKETO_MA_CONFIG_NAME);
$data = $config->getRawData();
unset($data['lead_source']);
$config->setData($data)->save();
}
/**
* Update lead fields map table to support centering around api name.
*/
function marketo_ma_update_8301() {
$connection = Database::getConnection();
$connection->delete('marketo_ma_lead_fields')
->condition('restName', '', 'is null')
->execute();
$schema = $connection->schema();
$schema->changeField('marketo_ma_lead_fields', 'restName', 'restName', [
'description' => 'Lead field key for REST API.',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
]);
$schema->addUniqueKey('marketo_ma_lead_fields', 'restName_unique_key', ['restName']);
}
/**
* Convert id to internal autoincrement field.
*/
function marketo_ma_update_8302() {
$connection = Database::getConnection();
$schema = $connection->schema();
$schema->changeField('marketo_ma_lead_fields', 'id', 'id', [
'description' => 'Internal identifier for field.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
]);
}
