api-8.x-1.x-dev/api.install
api.install
<?php
/**
* @file
* Install, update and uninstall functions for the api module.
*/
use Drupal\api\Entity\PhpBranch;
use Drupal\api\ExtendedQueries;
use Drupal\api\Parser;
use Drupal\api\StorageSchema\ApiContentStorageSchema;
use Drupal\api\Utilities;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\link\LinkItemInterface;
/**
* Implements hook_install().
*/
function api_install() {
$pathautoConfig = \Drupal::configFactory()->getEditable('pathauto.settings');
$enabled_entity_types = $pathautoConfig->get('enabled_entity_types') ?? [];
$enabled_entity_types[] = 'project';
$pathautoConfig->set('enabled_entity_types', $enabled_entity_types);
$pathautoConfig->set('punctuation.period', 2);
$pathautoConfig->set('punctuation.underscore', 2);
$pathautoConfig->save();
// Create new PHP branch.
/** @var \Drupal\api\Interfaces\PhpBranchInterface $php_branch */
$php_branch = PhpBranch::create();
$php_branch
->setTitle('PHP functions')
->setFunctionList('http://doc.php.net/downloads/json/php_manual_en.json')
->setFunctionUrlPattern('http://php.net/!function')
->setUpdateFrequency(Utilities::ONE_MONTH)
->save();
}
/**
* Implements hook_uninstall().
*/
function api_uninstall() {
$configFactory = \Drupal::configFactory();
// Remove config files that are no longer needed. Views are automatically
// detected and removed, but not the rest, so let's just delete them.
$configFactory->getEditable('api.settings')->delete();
$configFactory->getEditable('pathauto.pattern.projects')->delete();
$configFactory->getEditable('comment.type.api_comment')->delete();
if ($field_storage = FieldStorageConfig::loadByName('api_comment', 'field_api_comment_body')) {
$field_storage->delete();
}
$configFactory->getEditable('core.entity_form_display.comment.api_comment.default')->delete();
$configFactory->getEditable('core.entity_view_display.comment.api_comment.default')->delete();
$configFactory->getEditable('field.field.comment.api_comment.field_api_comment_body')->delete();
$configFactory->getEditable('field.storage.comment.field_api_comment_body')->delete();
// Alter existing config files that will still be present.
$pathautoConfig = $configFactory->getEditable('pathauto.settings');
$enabled_entity_types = $pathautoConfig->get('enabled_entity_types') ?? [];
$enabled_entity_types = array_diff(
$enabled_entity_types,
['project']
);
$pathautoConfig->set('enabled_entity_types', $enabled_entity_types);
$pathautoConfig->save();
// Empty any queues.
\Drupal::queue(Parser::QUEUE_PARSE)->deleteQueue();
\Drupal::queue('api_delete_related')->deleteQueue();
}
/**
* Ensure new storage schema class is linked to the entities.
*/
function api_update_9201() {
$entity_type_manager = \Drupal::entityTypeManager();
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$entities = ExtendedQueries::entityToTable();
foreach ($entities as $entity_id => $db_table) {
$entity_type = $entity_type_manager->getDefinition($entity_id)
->setHandlerClass('storage_schema', ApiContentStorageSchema::class);
$definition_update_manager->updateEntityType($entity_type);
}
}
/**
* Ensure new indexes are created.
*/
function api_update_9202() {
$entity_type_manager = \Drupal::entityTypeManager();
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$entities = ExtendedQueries::entityToTable();
foreach ($entities as $entity_id => $db_table) {
$entity_type = $entity_type_manager->getDefinition($entity_id);
$field_indexes = $entity_type->get('field_indexes') ?? [];
foreach ($field_indexes as $field_name) {
$definition_update_manager->updateFieldStorageDefinition($definition_update_manager->getFieldStorageDefinition($field_name, $entity_id));
}
}
}
/**
* Add the 'supported' base field to branch entities.
*/
function api_update_9203() {
$field = BaseFieldDefinition::create('boolean')
->setLabel(t('Supported'))
->setDescription(t('Whether this branch is supported. Pages from unsupported branches will display a message.'))
->setDefaultValue(FALSE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
])
->setDisplayConfigurable('form', TRUE);
\Drupal::entityDefinitionUpdateManager()->installFieldStorageDefinition(
'supported',
'branch',
'api',
$field
);
}
/**
* Add new field.
*/
function api_update_10001() {
$fields['git_url'] = BaseFieldDefinition::create('link')
->setLabel(t('Git URL'))
->setDescription(t('URL where to find the source code (e.g., https://git.drupalcode.org/project/drupal'))
->setSetting('title', \DRUPAL_DISABLED)
->setSetting('link_type', LinkItemInterface::LINK_EXTERNAL)
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'link',
'weight' => -5,
])
->setDisplayConfigurable('view', TRUE);
$update_manager = \Drupal::entityDefinitionUpdateManager();
foreach ($fields as $field_name => $field) {
$update_manager->installFieldStorageDefinition($field_name, 'project', 'project', $field);
}
}
/**
* Add new field.
*/
function api_update_10002() {
$fields['trusted'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Trusted'))
->setDescription(t('The code in this project is trusted and can be rendered safely as-is. <b>Do not use with projects you do not trust!</b>'))
->setDefaultValue(FALSE)
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'settings' => [
'display_label' => TRUE,
],
])
->setDisplayConfigurable('form', TRUE);
$update_manager = \Drupal::entityDefinitionUpdateManager();
foreach ($fields as $field_name => $field) {
$update_manager->installFieldStorageDefinition($field_name, 'project', 'project', $field);
}
}
