Skip to content

Commit

Permalink
Merge pull request Metadrop#125 from Eduardo-Morales-Alberti/fb-goto-…
Browse files Browse the repository at this point in the history
…translation

Allow declare language prefix on step go to
  • Loading branch information
omarlopesino authored Jan 4, 2022
2 parents f8b4726 + 67289f8 commit 13f5214
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Behat/Context/EntityContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,16 @@ public function goToTheLastEntityCreated($entity_type, $bundle = NULL, $subpath
* Go to a specific path of an entity with a specific label.
*
* @Given I go to the :entity_type entity with label :label
* @Given I go to the :entity_type entity with label :label in :language language
* @Given I go to :subpath of the :entity_type entity with label :label
*/
public function goToTheEntityWithLabel($entity_type, $label, $subpath = NULL) {
public function goToTheEntityWithLabel($entity_type, $label, $subpath = NULL, $language = NULL) {
$entity = $this->getCore()->loadEntityByLabel($entity_type, $label);
$path = $this->getCore()->buildEntityUri($entity_type, $entity, $subpath);
if ($language) {
$prefix = $this->getCore()->getLanguagePrefix($language);
$path = $prefix . '/' . $path;
}
if (!empty($path)) {
$this->getSession()->visit($this->locatePath($path));
}
Expand Down
11 changes: 11 additions & 0 deletions src/Behat/Cores/CoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,15 @@ public function formatString($string, array $params);
*/
public function createFileUrl($file, bool $relative = TRUE);

/**
* Obtain the language prefix from label.
*
* @param string $language
* Language.
*
* @return string
* Language prefix or empty if not found.
*/
public function getLanguagePrefix($language);

}
7 changes: 7 additions & 0 deletions src/Behat/Cores/Drupal7.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,11 @@ public function createFileUrl($file, bool $relative = TRUE) {
throw new PendingException('Pending to implement method in Drupal 7');
}

/**
* {@inheritdoc}
*/
public function getLanguagePrefix($language) {
throw new PendingException('Pending to implement method in Drupal 7');
}

}
21 changes: 21 additions & 0 deletions src/Behat/Cores/Drupal8.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,4 +469,25 @@ public function createFileUrl($file, bool $relative = TRUE) {
throw new InvalidArgumentException('%s method only accept %s objects in Drupal 8 or higher', __METHOD__, FileInterface::class);
}

/**
* {@inheritdoc}
*/
public function getLanguagePrefix($language) {
$language_manager = \Drupal::languageManager();
$language_list = $language_manager->getStandardLanguageList();

$filter_func = function ($item) use ($language) {
return in_array($language, $item);
};

$found = array_filter($language_list, $filter_func);

if (empty($found)) {
throw new \InvalidArgumentException(sprintf("Language %s not found", $language));
}

$prefixes = \Drupal::config('language.negotiation')->get('url.prefixes');
return $prefixes[array_key_first($found)];
}

}

0 comments on commit 13f5214

Please sign in to comment.