sector_contact-8.x-1.0-beta2/sector_contact.install
sector_contact.install
<?php
use Drupal\node\Entity\Node;
use Drupal\Core\Entity\EntityInterface;
use Drupal\block\Entity\Block;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
use Drupal\pathauto\PathautoState;
use Drupal\pathauto\PathautoGeneratorInterface;
use Drupal\pathauto\Entity\PathautoPattern;
/*
* Implements hook_uninstall()
*/
function sector_contact_uninstall() {
// Delete view
\Drupal::service('config.factory')->getEditable('views.view.sector_contact')->delete();
// Delete blocks
$page_block = Block::load('sector_views_block__sector_contact_contact_page_block');
if($page_block) {
$page_block->delete();
}
// Delete view page
$sector_page = \Drupal::service('entity.repository')->loadEntityByUuid('node', '98639bb5-f8be-48fd-8012-1e17c79a918f');
if($sector_page) {
$sector_page->delete();
}
// Delete content type.
$content_type = \Drupal::entityTypeManager()->getStorage('node_type')->load('sector_contact');
$content_type->delete();
$vocabularies = [
'sector_contact_unit_dept',
'sector_contact_category'
];
foreach($vocabularies as $vocabulary_id) {
$vocabulary = Vocabulary::load($vocabulary_id);
if($vocabulary) {
$vocabulary->delete();
}
}
// Delete all nodes of given content type.
$storage_handler = \Drupal::entityTypeManager()->getStorage('node');
$nodes = $storage_handler->loadByProperties(['type' => 'sector_contact']);
$storage_handler->delete($nodes);
// delete pathauto pattern
$storage = \Drupal::entityTypeManager()->getStorage('pathauto_pattern');
$configuration = $storage->load('sector_contact');
if ($configuration && $configuration instanceof PathautoPattern) {
// Delete the configuration.
$configuration->delete();
// Clear the Pathauto state and regenerate aliases if needed.
\Drupal::service('pathauto.generator')->generateEntityAlias($configuration->getEntityType(), $configuration->getEntity(), PathautoGeneratorInterface::OP_UPDATE);
// Inform the user that the configuration was deleted successfully.
drupal_set_message(t('The Pathauto configuration has been deleted successfully.'));
}
}
/**
* Implements hook_ENTITY_TYPE_insert() for node entities.
*/
function sector_contact_node_insert(EntityInterface $entity) {
switch ($entity->uuid()) {
case '98639bb5-f8be-48fd-8012-1e17c79a918f':
// find block
$block = Drupal\block\Entity\Block::load('sector_views_block__sector_contact_contact_page_block');
if($block) {
$block->setVisibilityConfig('request_path', array('id' => 'request_path',
'pages'=> '/node/' . $entity->id(),
'negate' => false,
)
);
$block->save();
$activeThemeName = \Drupal::config('system.theme')->get('default');
$new_id = str_replace('sector', $activeThemeName, $block->get('id'));
$child_block = $block->createDuplicateBlock($new_id, $activeThemeName);
$child_block->save();
\Drupal::logger('sector_contact')->notice('Setting contact page view block to appear on #%nid', [
'%nid' => $entity->id(),
]);
}
break;
}
}
