How Can I Check If I Am Working With A Translation In Node::preSave() With Feeds?
Introduction
When working with translation-enabled entities in Drupal, it's essential to understand how to differentiate between the original node and its translations. This becomes particularly crucial when you need to perform specific actions based on whether the node being saved is a translation or not. In this article, we'll explore how to check if you're working with a translation in the node::preSave()
method, specifically when using the Feeds module.
Understanding Translation in Drupal
Before diving into the code, let's quickly review how translation works in Drupal. When you enable translation for an entity, Drupal creates a new entity for each translation. This new entity is a child of the original entity and shares the same bundle. The translation entity has a language
field that specifies the language of the translation.
NodeBundle Custom Entity Bundle
You've created a custom entity bundle called NodeBundle
and overridden the preSave()
method. This method is called before the node is saved, making it an ideal place to perform actions based on whether the node is a translation or not.
Checking if a Node is a Translation
To check if a node is a translation, you can use the isTranslation()
method provided by the Entity
class. This method returns TRUE
if the entity is a translation and FALSE
otherwise.
Here's an example of how you can use this method in your preSave()
method:
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
class NodeBundle extends EntityBundleBase {
/**
* {@inheritdoc}
*/
public function preSave(EntityInterface $entity) {
parent::preSave($entity);
// Check if the node is a translation.
if ($entity->isTranslation()) {
// Perform actions specific to translations.
$this->processTranslationField($entity);
} else {
// Perform actions specific to the original node.
$this->processOriginalField($entity);
}
}
/**
* Process the translation field.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity being saved.
*/
protected function processTranslationField(EntityInterface $entity) {
// Add your code here to process the translation field.
}
/**
* Process the original field.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity being saved.
*/
protected function processOriginalField(EntityInterface $entity) {
// Add your code here to process the original field.
}
}
In this example, we first call the parent preSave()
method to ensure that any necessary actions are performed by the parent class. Then, we check if the node is a translation using the isTranslation()
method. If it is, we call the processTranslationField()
method to perform actions specific to translations. If it's not a translation, we call the processOriginalField()
method to perform actions specific to the original node.
Using the Feeds Module
When using the Feeds module, you may need to perform actions based on whether the node being saved is a translation or not. The Feeds module provides a feeds_entity_presave()
hook that allows you to perform actions before the entity is saved.
Here's an example of how you can use this hook to check if a node is a translation:
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManagerInterface;
function mymodule_feeds_entity_presave(EntityInterface $entity) {
// Check if the node is a translation.
if ($entity->isTranslation()) {
// Perform actions specific to translations.
$this->processTranslationField($entity);
} else {
// Perform actions specific to the original node.
$this->processOriginalField($entity);
}
}
In this example, we use the feeds_entity_presave()
hook to perform actions before the entity is saved. We check if the node is a translation using the isTranslation()
method and perform actions specific to translations or the original node accordingly.
Conclusion
In this article, we've explored how to check if you're working with a translation in the node::preSave()
method, specifically when using the Feeds module. We've seen how to use the isTranslation()
method to determine whether a node is a translation or not and perform actions accordingly. By following the examples provided, you should be able to implement this functionality in your own custom entity bundle and Feeds module configuration.
Additional Resources
- Drupal Core API: EntityInterface
- Drupal Core API: EntityManagerInterface
- Feeds Module API: feeds_entity_presave()
Q&A: Working with Translations in Node::preSave() with Feeds =============================================================
Introduction
In our previous article, we explored how to check if you're working with a translation in the node::preSave()
method, specifically when using the Feeds module. In this article, we'll answer some frequently asked questions related to working with translations in Node::preSave() with Feeds.
Q: What is the difference between isTranslation()
and getTranslation()
?
A: The isTranslation()
method checks if the entity is a translation, while the getTranslation()
method returns the translation entity if it exists. If the entity is not a translation, getTranslation()
returns NULL
.
Q: How do I get the original node when working with translations?
A: To get the original node when working with translations, you can use the getOriginal()
method. This method returns the original node, regardless of whether the current entity is a translation or not.
Q: Can I use isTranslation()
in a custom entity bundle?
A: Yes, you can use isTranslation()
in a custom entity bundle. However, you need to make sure that your custom entity bundle is properly configured to support translations.
Q: How do I handle multiple translations of the same node?
A: When working with multiple translations of the same node, you can use the getTranslation()
method to retrieve the translation entity. You can then use the getOriginal()
method to get the original node.
Q: Can I use feeds_entity_presave()
in a custom module?
A: Yes, you can use feeds_entity_presave()
in a custom module. However, you need to make sure that your custom module is properly configured to use the Feeds module.
Q: How do I debug issues related to translations in Node::preSave()?
A: To debug issues related to translations in Node::preSave(), you can use the drupal_debug()
function to print out the entity and its translation status. You can also use the devel
module to inspect the entity and its translation status.
Q: Can I use isTranslation()
in a hook?
A: Yes, you can use isTranslation()
in a hook. However, you need to make sure that the hook is properly configured to use the isTranslation()
method.
Q: How do I handle translation-related errors in Node::preSave()?
A: To handle translation-related errors in Node::preSave(), you can use a try-catch block to catch any exceptions that may occur when working with translations. You can then use the drupal_set_message()
function to display an error message to the user.
Conclusion
In this article, we've answered some frequently asked questions related to working with translations in Node::preSave() with Feeds. We've covered topics such as the difference between isTranslation()
and getTranslation()
, how to get the original node when working with translations, and how to handle multiple translations of the same node. By following the examples provided, you should be able to implement this functionality in your own custom entity bundle and Feeds module configuration.