function bynder_update_8008 in Bynder 8.2
Same name and namespace in other branches
- 8.3 bynder.install \bynder_update_8008()
- 4.0.x bynder.install \bynder_update_8008()
Creates bynder_metadata field and set the update frequency.
File
- ./
bynder.install, line 133 - Install, uninstall and update hooks for Bynder module.
Code
function bynder_update_8008() {
// Clear field type plugin caches to allow the new field to be installed.
\Drupal::service('plugin.manager.field.field_type')
->clearCachedDefinitions();
// Update local metadata information once per week (in seconds).
\Drupal::configFactory()
->getEditable('bynder.settings')
->set('update_frequency', 7 * 24 * 60 * 60)
->save();
// Add the action config entity to update the Bynder metadata.
if (!Action::load('bynder_metadata')) {
$action = Action::create([
'id' => 'bynder_metadata',
'label' => 'Update Bynder metadata',
'type' => 'media',
'plugin' => 'bynder_metadata',
]);
$action
->save();
}
// Do nothing if the field already exists.
if ($metadata_field_storage = FieldStorageConfig::loadByName('media', BynderMetadataItem::METADATA_FIELD_NAME)) {
if ($metadata_field_storage
->getType() !== 'bynder_metadata') {
throw new UpdateException('The "bynder_metadata" media field already exists. Please remove the it before running the update function.');
}
return;
}
$entity_type_manager = \Drupal::entityTypeManager();
$metadata_field_storage = NULL;
// Create a field instance for each of the Bynder media types.
foreach (\Drupal::service('bynder')
->getBynderMediaTypes() as $media_type_id => $media_type) {
/** @var \Drupal\bynder\Plugin\media\Source\Bynder $source */
$source = $media_type
->getSource();
if (!$metadata_field_storage) {
$metadata_field_storage = $source
->createMetadataFieldStorage();
$metadata_field_storage
->save();
}
$metadata_field = $source
->createMetadataField($media_type);
$metadata_field
->save();
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
$form_display = $entity_type_manager
->getStorage('entity_form_display')
->load("media.{$media_type_id}.default");
if ($form_display) {
$form_display
->removeComponent(BynderMetadataItem::METADATA_FIELD_NAME)
->save();
}
/** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */
$view_display = $entity_type_manager
->getStorage('entity_view_display')
->load("media.{$media_type_id}.default");
if ($view_display) {
$view_display
->removeComponent(BynderMetadataItem::METADATA_FIELD_NAME)
->save();
}
}
}