Skip to content

Commit

Permalink
Merge pull request #1632 from vbartusevicius/v2.4.x
Browse files Browse the repository at this point in the history
2.4.x compatibility for PR#1631
  • Loading branch information
l3pp4rd authored Jul 15, 2016
2 parents 8f172af + 3d2133c commit dc1c7a9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
42 changes: 27 additions & 15 deletions lib/Gedmo/Translatable/Mapping/Driver/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function readExtendedMetadata($meta, array &$config)
$xml = $xml->children(self::GEDMO_NAMESPACE_URI);

if (($xmlDoctrine->getName() == 'entity' || $xmlDoctrine->getName() == 'mapped-superclass')) {
if (isset($xml->translation)) {
if ($xml->count() && isset($xml->translation)) {
/**
* @var \SimpleXmlElement $data
*/
Expand All @@ -53,8 +53,14 @@ public function readExtendedMetadata($meta, array &$config)

if (property_exists($meta, 'embeddedClasses') && $meta->embeddedClasses) {
foreach ($meta->embeddedClasses as $propertyName => $embeddedClassInfo) {
$xmlEmbbededClass = $this->_getMapping($embeddedClassInfo['class']);
$this->inspectElementsForTranslatableFields($xmlEmbbededClass, $config, $propertyName);
$xmlEmbeddedClass = $this->_getMapping($embeddedClassInfo['class']);
$this->inspectElementsForTranslatableFields($xmlEmbeddedClass, $config, $propertyName);
}
}

if ($xmlDoctrine->{'attribute-overrides'}->count() > 0) {
foreach ($xmlDoctrine->{'attribute-overrides'}->{'attribute-override'} as $overrideMapping) {
$this->buildFieldConfiguration($this->_getAttribute($overrideMapping, 'name'), $overrideMapping->field, $config);
}
}

Expand All @@ -75,18 +81,24 @@ private function inspectElementsForTranslatableFields(\SimpleXMLElement $xml, ar

foreach ($xml->field as $mapping) {
$mappingDoctrine = $mapping;
/**
* @var \SimpleXmlElement $mapping
*/
$mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
$field = null !== $prefix ? $prefix . '.' . $this->_getAttribute($mappingDoctrine, 'name') : $this->_getAttribute($mappingDoctrine, 'name');
if (isset($mapping->translatable)) {
$config['fields'][] = $field;
/** @var \SimpleXmlElement $data */
$data = $mapping->translatable;
if ($this->_isAttributeSet($data, 'fallback')) {
$config['fallback'][$field] = 'true' == $this->_getAttribute($data, 'fallback') ? true : false;
}

$fieldName = $this->_getAttribute($mappingDoctrine, 'name');
if ($prefix !== null) {
$fieldName = $prefix . '.' . $fieldName;
}
$this->buildFieldConfiguration($fieldName, $mapping, $config);
}
}

private function buildFieldConfiguration($fieldName, \SimpleXMLElement $mapping, array &$config)
{
$mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
if ($mapping->count() > 0 && isset($mapping->translatable)) {
$config['fields'][] = $fieldName;
/** @var \SimpleXmlElement $data */
$data = $mapping->translatable;
if ($this->_isAttributeSet($data, 'fallback')) {
$config['fallback'][$fieldName] = 'true' == $this->_getAttribute($data, 'fallback') ? true : false;
}
}
}
Expand Down
30 changes: 21 additions & 9 deletions lib/Gedmo/Translatable/Mapping/Driver/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,16 @@ public function readExtendedMetadata($meta, array &$config)
$config['locale'] = $classMapping['translation']['language'];
}
}

if (isset($mapping['fields'])) {
foreach ($mapping['fields'] as $field => $fieldMapping) {
if (isset($fieldMapping['gedmo'])) {
if (in_array('translatable', $fieldMapping['gedmo']) || isset($fieldMapping['gedmo']['translatable'])) {
// fields cannot be overrided and throws mapping exception
$config['fields'][] = $field;
if (isset($fieldMapping['gedmo']['translatable']['fallback'])) {
$config['fallback'][$field] = $fieldMapping['gedmo']['translatable']['fallback'];
}
}
}
$this->buildFieldConfiguration($field, $fieldMapping, $config);
}
}

if (isset($mapping['attributeOverride'])) {
foreach ($mapping['attributeOverride'] as $field => $overrideMapping) {
$this->buildFieldConfiguration($field, $overrideMapping, $config);
}
}

Expand All @@ -73,4 +72,17 @@ protected function _loadMappingFile($file)
{
return \Symfony\Component\Yaml\Yaml::parse(file_get_contents($file));
}

private function buildFieldConfiguration($field, array $fieldMapping, array &$config)
{
if (isset($fieldMapping['gedmo'])) {
if (in_array('translatable', $fieldMapping['gedmo']) || isset($fieldMapping['gedmo']['translatable'])) {
// fields cannot be overrided and throws mapping exception
$config['fields'][] = $field;
if (isset($fieldMapping['gedmo']['translatable']['fallback'])) {
$config['fallback'][$field] = $fieldMapping['gedmo']['translatable']['fallback'];
}
}
}
}
}

0 comments on commit dc1c7a9

Please sign in to comment.