Skip to content

Commit

Permalink
Add submit function and hook-update to copy default country to event …
Browse files Browse the repository at this point in the history
…and group location field
  • Loading branch information
vcsvinicius authored and robertragas committed Aug 23, 2024
1 parent b4e49c9 commit 6400391
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 0 deletions.
46 changes: 46 additions & 0 deletions modules/social_features/social_event/social_event.install
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Install, update and uninstall functions for the social_event module.
*/

use Drupal\field\Entity\FieldConfig;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\user\RoleInterface;

Expand Down Expand Up @@ -250,3 +251,48 @@ function social_event_update_130003() : string {
// Output logged messages to related channel of update execution.
return $updater->logger()->output();
}

/**
* Update default location from system configuration to event.
*/
function social_event_update_130004(): void {
$default_country = \Drupal::config('system.date')->get('country.default');
$event_address_field = FieldConfig::loadByName('node', 'event', 'field_event_address');
if (empty($event_address_field)) {
return;
}

// Init and fill default values variable.
$default_values = [];
if (!empty($default_country)) {
$default_values = $event_address_field->getDefaultValueLiteral();
// When the default values isn't initiated, so init it.
if (empty($default_values)) {
$default_values[] = [
'langcode' => NULL,
'administrative_area' => "",
'locality' => "",
'dependent_locality' => NULL,
'postal_code' => "",
'sorting_code' => NULL,
'address_line1' => "",
'address_line2' => NULL,
'address_line3' => NULL,
'organization' => NULL,
'given_name' => NULL,
'additional_name' => NULL,
'family_name' => NULL,
];

}

// Add country code from site default country.
$default_values[0]['country_code'] = $default_country;
}

// Save field with new default value.
if (method_exists($event_address_field, 'setDefaultValue')) {
$event_address_field->setDefaultValue($default_values)
->save();
}
}
61 changes: 61 additions & 0 deletions modules/social_features/social_event/social_event.module
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\group\Entity\GroupRelationship;
use Drupal\group\GroupMembershipLoaderInterface;
use Drupal\menu_link_content\MenuLinkContentInterface;
Expand Down Expand Up @@ -1511,3 +1512,63 @@ function _social_event_menu_local_tasks_routes(): array {

return $routes;
}

/**
* Add a submit function to update default country for event location field.
*
* Implements hook_form_FORM_ID_alter().
*/
function social_event_form_system_regional_settings_alter(array &$form): void {
$form['#submit'][] = '_social_event_system_regional_settings_submit';
}

/**
* Submit function to save default country to event location field.
*
* @param array $form
* Nested array of form elements that comprise the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function _social_event_system_regional_settings_submit(array &$form, FormStateInterface $form_state): void {
// Load field configuration and return early if it isn't exist.
$event_address_field = FieldConfig::loadByName('node', 'event', 'field_event_address');
if (empty($event_address_field)) {
return;
}

// Init and fill default values variable.
$default_values = [];
if (!empty($form_state->getValue('site_default_country'))) {
$default_values = $event_address_field->getDefaultValueLiteral();
// When the default values isn't initiated, so init it.
if (empty($default_values)) {
$default_values[] = [
'langcode' => NULL,
'administrative_area' => "",
'locality' => "",
'dependent_locality' => NULL,
'postal_code' => "",
'sorting_code' => NULL,
'address_line1' => "",
'address_line2' => NULL,
'address_line3' => NULL,
'organization' => NULL,
'given_name' => NULL,
'additional_name' => NULL,
'family_name' => NULL,
];
}

// Add country code from site default country.
$default_values[0]['country_code'] = $form_state->getValue('site_default_country');
}

// Save field with new default value.
if (method_exists($event_address_field, 'setDefaultValue')) {
$event_address_field->setDefaultValue($default_values)
->save();
}
}
45 changes: 45 additions & 0 deletions modules/social_features/social_group/social_group.install
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\group\Entity\GroupType;
use Drupal\menu_link_content\Entity\MenuLinkContent;
Expand Down Expand Up @@ -970,3 +971,47 @@ function _social_group_update_permissions(mixed $group_type, string $permission)
$group_role->save();
}
}

/**
* Update default location from system configuration to group.
*/
function social_group_update_13013(): void {
$default_country = \Drupal::config('system.date')->get('country.default');
$group_address_field = FieldConfig::loadByName('group', 'flexible_group', 'field_group_address');
if (empty($group_address_field)) {
return;
}

// Init and fill default values variable.
$default_values = [];
if (!empty($default_country)) {
$default_values = $group_address_field->getDefaultValueLiteral();
// When the default values isn't initiated, so init it.
if (empty($default_values)) {
$default_values[] = [
'langcode' => NULL,
'administrative_area' => "",
'locality' => "",
'dependent_locality' => NULL,
'postal_code' => "",
'sorting_code' => NULL,
'address_line1' => "",
'address_line2' => NULL,
'address_line3' => NULL,
'organization' => NULL,
'given_name' => NULL,
'additional_name' => NULL,
'family_name' => NULL,
];
}

// Add country code from site default country.
$default_values[0]['country_code'] = $default_country;
}

// Save field with new default value.
if (method_exists($group_address_field, 'setDefaultValue')) {
$group_address_field->setDefaultValue($default_values)
->save();
}
}
61 changes: 61 additions & 0 deletions modules/social_features/social_group/social_group.module
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use Drupal\Core\Render\Element;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldConfig;
use Drupal\group\Entity\GroupRelationship;
use Drupal\group\Entity\GroupRelationshipInterface;
use Drupal\group\Entity\GroupRelationshipType;
Expand Down Expand Up @@ -2861,3 +2862,63 @@ function social_group_views_pre_view(ViewExecutable $view, string $display_id, a
function social_group_group_types(): array {
return array_fill_keys(['node', 'user'], TRUE);
}

/**
* Add a submit function to update default country for group location field.
*
* Implements hook_form_FORM_ID_alter().
*/
function social_group_form_system_regional_settings_alter(array &$form): void {
$form['#submit'][] = '_social_group_system_regional_settings_submit';
}

/**
* Submit function to save default country to group location field.
*
* @param array $form
* Nested array of form elements that comprise the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
function _social_group_system_regional_settings_submit(array &$form, FormStateInterface $form_state): void {
// Load field configuration and return early if it isn't exist.
$group_address_field = FieldConfig::loadByName('group', 'flexible_group', 'field_group_address');
if (empty($group_address_field)) {
return;
}

// Init and fill default values variable.
$default_values = [];
if (!empty($form_state->getValue('site_default_country'))) {
$default_values = $group_address_field->getDefaultValueLiteral();
// When the default values isn't initiated, so init it.
if (empty($default_values)) {
$default_values[] = [
'langcode' => NULL,
'administrative_area' => "",
'locality' => "",
'dependent_locality' => NULL,
'postal_code' => "",
'sorting_code' => NULL,
'address_line1' => "",
'address_line2' => NULL,
'address_line3' => NULL,
'organization' => NULL,
'given_name' => NULL,
'additional_name' => NULL,
'family_name' => NULL,
];
}

// Add country code from site default country.
$default_values[0]['country_code'] = $form_state->getValue('site_default_country');
}

// Save field with new default value.
if (method_exists($group_address_field, 'setDefaultValue')) {
$group_address_field->setDefaultValue($default_values)
->save();
}
}

0 comments on commit 6400391

Please sign in to comment.