Added action to update, removed farm field factory in install

This commit is contained in:
Jotham Gates 2022-05-02 22:17:47 +10:00
parent ba346ab3bb
commit edc7df906b
1 changed files with 58 additions and 6 deletions

View File

@ -4,30 +4,82 @@
* @file
* Updates farm_owner module.
*/
use Drupal\system\Entity\Action;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
/**
/**
* Add 'owner' field to assets.
*/
function farm_owner_update_9001() {
// function hook_post_update_add_asset_owner() {
$field_name = 'owner';
$field_info = [
'type' => 'entity_reference',
'label' => t('Assigned to'),
'description' => t('Optionally assign this task to one or more people.'),
'target_type' => 'user',
'multiple' => TRUE,
'weight' => [
'form' => -70,
'view' => -70,
'form' => -70,
'view' => -70,
],
];
$field_storage_definition = \Drupal::service('farm_field.factory')->baseFieldDefinition($field_info);
$handler = 'default:user';
$handler_settings = [
'include_anonymous' => FALSE,
'filter' => [
'type' => '_none',
],
'target_bundles' => NULL,
'sort' => [
'field' => '_none',
],
'auto_create' => FALSE,
];
$form_display_options = [
'type' => 'options_select',
'weight' => $options['weight']['form'] ?? 0,
];
$view_display_options = [
'label' => 'inline',
'type' => 'entity_reference_label',
'weight' => $options['weight']['view'] ?? 0,
'settings' => [
'link' => TRUE,
],
];
$field_storage_definition = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Assigned to'))
->setDescription(t('Optionally assign this task to one or more people.'))
->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
->setSetting('target_type', 'user')
->setSetting('handler', $handler)
->setSetting('handler_settings', $handler_settings)
->setDisplayOptions('form', $form_display_options)
->setDisplayOptions('view', $view_display_options);
// $field_storage_definition = \Drupal::service('farm_field.factory')->baseFieldDefinition($field_info);
$entity_type = 'asset';
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition($field_name, $entity_type, $entity_type, $field_storage_definition);
// Create action for assigning assets to users.
$action = Action::create([
'id' => 'asset_assign_action',
'label' => t('Assign asset'),
'type' => 'asset',
'plugin' => 'asset_assign_action',
'configuration' => [],
]);
$action->save();
}
/*
* Add the assign owner to asset configuration for bulk assigns
*/
// TODO: function hook_post_update_()
// function hook_post_update_add_asset_owner() {
// }