Skip to content

Commit

Permalink
Improve code with assignments from pull-request
Browse files Browse the repository at this point in the history
  • Loading branch information
vcsvinicius authored and denis-getopensocial committed Nov 19, 2024
1 parent 0ab6852 commit a459f6a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function social_content_report_update_130002(): void {
$form_display = $storage->load('taxonomy_term.report_reasons.default');

// When entity-form-display isn't be created,
// it'll create from install folder.
// It will be created from the install folder.
if (empty($form_display)) {
// Get file from install folder and check if it exists.
$config_file = \Drupal::service('extension.list.module')->getPath('social_content_report') . '/config/install/core.entity_form_display.taxonomy_term.report_reasons.default.yml';
Expand All @@ -129,7 +129,7 @@ function social_content_report_update_130002(): void {
->save();
}
else {
// Get fields from entity-form-display and check if path already be hidden.
// Get fields and check if path is already be hidden.
$hidden = $form_display->get('hidden');
$content = $form_display->get('content');
if (in_array('path', array_keys($hidden))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function social_event_type_update_130002(): void {
return;
}

// Get fields from entity-form-display and check if path already be hidden.
// Get fields and check if path is already be hidden.
$hidden = $form_display->get('hidden');
$content = $form_display->get('content');
if (in_array('path', array_keys($hidden))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ function social_group_flexible_group_update_130008(): void {
return;
}

// Get fields from entity-form-display and check if path already be hidden.
// Get fields and check if path is already be hidden.
$hidden = $form_display->get('hidden');
$content = $form_display->get('content');
if (in_array('path', array_keys($hidden))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ function social_profile_organization_tag_update_130002(): void {
$form_display = $storage->load('taxonomy_term.profile_organization_tag.default');

// When entity-form-display isn't be created,
// it'll create from install folder.
// It will be created from the install folder.
if (empty($form_display)) {
// Get file from install folder and check if it exists.
$config_file = \Drupal::service('extension.list.module')->getPath('social_profile_organization_tag') . '/config/install/core.entity_form_display.taxonomy_term.profile_organization_tag.default.yml';
Expand All @@ -184,7 +184,7 @@ function social_profile_organization_tag_update_130002(): void {
->save();
}
else {
// Get fields from entity-form-display and check if path already be hidden.
// Get fields and check if path is already be hidden.
$hidden = $form_display->get('hidden');
$content = $form_display->get('content');
if (in_array('path', array_keys($hidden))) {
Expand Down
112 changes: 28 additions & 84 deletions modules/social_features/social_profile/social_profile.install
Original file line number Diff line number Diff line change
Expand Up @@ -231,116 +231,60 @@ function social_profile_update_130002(): void {
}

/**
* Create entity-form-display or hidden path field from Profile Tag taxonomy.
* Hide path field from Profile Tag, Interests and Expertise taxonomies.
*/
function social_profile_update_130003(): void {
$storage = \Drupal::entityTypeManager()->getStorage('entity_form_display');
$form_display = $storage->load('taxonomy_term.profile_tag.default');

// When entity-form-display isn't be created,
// it'll create from install folder.
if (empty($form_display)) {
// Get file from install folder and check if it exists.
$config_file = \Drupal::service('extension.list.module')->getPath('social_profile') . '/config/install/core.entity_form_display.taxonomy_term.profile_tag.default.yml';
if (file_exists($config_file) === FALSE) {
\Drupal::logger('social_profile')->error('Configuration file for entity-form-display "profile_tag" not found.');
return;
}

// Apply the configuration to create entity-form-display.
$view_config = Yaml::parseFile($config_file);
\Drupal::configFactory()->getEditable('core.entity_form_display.taxonomy_term.profile_tag.default')
->setData($view_config)
->save();
}
else {
// Get fields from entity-form-display and check if path already be hidden.
$hidden = $form_display->get('hidden');
$content = $form_display->get('content');
if (in_array('path', array_keys($hidden))) {
\Drupal::logger('social_profile')->info('The path field already is hidden on Profile Tag taxonomy.');
return;
}

// Manipulate path field to be hidden and save.
$hidden['path'] = TRUE;
unset($content['path']);
$form_display->set('hidden', $hidden)
->set('content', $content)
->save();
}
}

/**
* Create entity-form-display or hidden path field from Interests taxonomy.
*/
function social_profile_update_130004(): void {
$storage = \Drupal::entityTypeManager()->getStorage('entity_form_display');
$form_display = $storage->load('taxonomy_term.interests.default');

// When entity-form-display isn't be created,
// it'll create from install folder.
if (empty($form_display)) {
// Get file from install folder and check if it exists.
$config_file = \Drupal::service('extension.list.module')->getPath('social_profile') . '/config/install/core.entity_form_display.taxonomy_term.interests.default.yml';
if (file_exists($config_file) === FALSE) {
\Drupal::logger('social_profile')->error('Configuration file for entity-form-display "interests" not found.');
return;
}

// Apply the configuration to create entity-form-display.
$view_config = Yaml::parseFile($config_file);
\Drupal::configFactory()->getEditable('core.entity_form_display.taxonomy_term.interests.default')
->setData($view_config)
->save();
}
else {
// Get fields from entity-form-display and check if path already be hidden.
$hidden = $form_display->get('hidden');
$content = $form_display->get('content');
if (in_array('path', array_keys($hidden))) {
\Drupal::logger('social_profile')->info('The path field already is hidden on Interests taxonomy.');
return;
}
$taxonomies = [
'profile_tag' => 'Profile Tag',
'interests' => 'Interests',
'expertise' => 'Expertise',
];

// Manipulate path field to be hidden and save.
$hidden['path'] = TRUE;
unset($content['path']);
$form_display->set('hidden', $hidden)
->set('content', $content)
->save();
foreach ($taxonomies as $taxonomy_id => $taxonomy_name) {
_social_profile_hide_path_field_from_entity_form_display($taxonomy_id, $taxonomy_name);
}
}

/**
* Create entity-form-display or hidden path field from Expertise taxonomy.
* Create entity-form-display or hidden path field from a taxonomy.
*
* @param string $taxonomy_id
* Taxonomy ID.
* @param string $taxonomy_name
* Taxonomy Label (Name).
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
function social_profile_update_130005(): void {
function _social_profile_hide_path_field_from_entity_form_display(string $taxonomy_id, string $taxonomy_name): void {
$storage = \Drupal::entityTypeManager()->getStorage('entity_form_display');
$form_display = $storage->load('taxonomy_term.expertise.default');
$form_display = $storage->load(sprintf('taxonomy_term.%s.default', $taxonomy_id));

// When entity-form-display isn't be created,
// it'll create from install folder.
// It will be created from the install folder.
if (empty($form_display)) {
// Get file from install folder and check if it exists.
$config_file = \Drupal::service('extension.list.module')->getPath('social_profile') . '/config/install/core.entity_form_display.taxonomy_term.expertise.default.yml';
$config_file = \Drupal::service('extension.list.module')->getPath('social_profile') . sprintf('/config/install/core.entity_form_display.taxonomy_term.%s.default.yml', $taxonomy_id);
if (file_exists($config_file) === FALSE) {
\Drupal::logger('social_profile')->error('Configuration file for entity-form-display "expertise" not found.');
$message = sprintf('Configuration file for entity-form-display "%s" not found.', $taxonomy_id);
\Drupal::logger('social_profile')->error($message);
return;
}

// Apply the configuration to create entity-form-display.
$view_config = Yaml::parseFile($config_file);
\Drupal::configFactory()->getEditable('core.entity_form_display.taxonomy_term.expertise.default')
\Drupal::configFactory()
->getEditable(sprintf('core.entity_form_display.taxonomy_term.%s.default', $taxonomy_id))
->setData($view_config)
->save();
}
else {
// Get fields from entity-form-display and check if path already be hidden.
// Get fields and check if path is already be hidden.
$hidden = $form_display->get('hidden');
$content = $form_display->get('content');
if (in_array('path', array_keys($hidden))) {
\Drupal::logger('social_profile')->info('The path field already is hidden on Expertise taxonomy.');
$message = sprintf('The path field already is hidden on %s taxonomy.', $taxonomy_name);
\Drupal::logger('social_profile')->info($message);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion modules/social_features/social_topic/social_topic.install
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ function social_topic_update_130003(): void {
return;
}

// Get fields from entity-form-display and check if path already be hidden.
// Get fields and check if path is already be hidden.
$hidden = $form_display->get('hidden');
$content = $form_display->get('content');
if (in_array('path', array_keys($hidden))) {
Expand Down

0 comments on commit a459f6a

Please sign in to comment.