Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if FieldList has tabset before trying to add cms fields to tabs. #248

Merged
merged 1 commit into from Aug 1, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
120 changes: 61 additions & 59 deletions code/model/Translatable.php
Expand Up @@ -1065,72 +1065,74 @@ function updateCMSFields(FieldList $fields) {
$alreadyTranslatedLocales[$this->owner->Locale] = $this->owner->Locale;
$alreadyTranslatedLocales = array_combine($alreadyTranslatedLocales, $alreadyTranslatedLocales);

// Check if fields exist already to avoid adding them twice on repeat invocations
$tab = $fields->findOrMakeTab('Root.Translations', _t('Translatable.TRANSLATIONS', 'Translations'));
if(!$tab->fieldByName('CreateTransHeader')) {
$tab->push(new HeaderField(
'CreateTransHeader',
_t('Translatable.CREATE', 'Create new translation'),
2
));
}
if(!$tab->fieldByName('NewTransLang') && !$tab->fieldByName('AllTransCreated')) {
$langDropdown = LanguageDropdownField::create(
"NewTransLang",
_t('Translatable.NEWLANGUAGE', 'New language'),
$alreadyTranslatedLocales,
'SiteTree',
'Locale-English',
$this->owner
)->addExtraClass('languageDropdown no-change-track');
$tab->push($langDropdown);
$canAddLocale = (count($langDropdown->getSource()) > 0);

if($canAddLocale) {
// Only add create button if new languages are available
$tab->push(
$createButton = InlineFormAction::create(
'createtranslation',
_t('Translatable.CREATEBUTTON', 'Create')
)->addExtraClass('createTranslationButton')
);
$createButton->includeDefaultJS(false); // not fluent API...
} else {
$tab->removeByName('NewTransLang');
$tab->push(new LiteralField(
'AllTransCreated',
_t('Translatable.ALLCREATED', 'All allowed translations have been created.')
));
}
}
if($alreadyTranslatedLocales) {
if(!$tab->fieldByName('ExistingTransHeader')) {
if ($fields->hasTabSet()) {
// Check if fields exist already to avoid adding them twice on repeat invocations
$tab = $fields->findOrMakeTab('Root.Translations', _t('Translatable.TRANSLATIONS', 'Translations'));
if(!$tab->fieldByName('CreateTransHeader')) {
$tab->push(new HeaderField(
'ExistingTransHeader',
_t('Translatable.EXISTING', 'Existing translations'),
3
'CreateTransHeader',
_t('Translatable.CREATE', 'Create new translation'),
2
));
if (!$tab->fieldByName('existingtrans')) {
$existingTransHTML = '<ul>';
if ($existingTranslations = $this->getTranslations()) {
foreach ($existingTranslations as $existingTranslation) {
if ($existingTranslation && $existingTranslation->hasMethod('CMSEditLink')) {
$existingTransHTML .= sprintf(
'<li><a href="%s">%s</a></li>',
Controller::join_links(
$existingTranslation->CMSEditLink(),
'?Locale=' . $existingTranslation->Locale
),
i18n::get_locale_name($existingTranslation->Locale)
);
}
if(!$tab->fieldByName('NewTransLang') && !$tab->fieldByName('AllTransCreated')) {
$langDropdown = LanguageDropdownField::create(
"NewTransLang",
_t('Translatable.NEWLANGUAGE', 'New language'),
$alreadyTranslatedLocales,
'SiteTree',
'Locale-English',
$this->owner
)->addExtraClass('languageDropdown no-change-track');
$tab->push($langDropdown);
$canAddLocale = (count($langDropdown->getSource()) > 0);

if($canAddLocale) {
// Only add create button if new languages are available
$tab->push(
$createButton = InlineFormAction::create(
'createtranslation',
_t('Translatable.CREATEBUTTON', 'Create')
)->addExtraClass('createTranslationButton')
);
$createButton->includeDefaultJS(false); // not fluent API...
} else {
$tab->removeByName('NewTransLang');
$tab->push(new LiteralField(
'AllTransCreated',
_t('Translatable.ALLCREATED', 'All allowed translations have been created.')
));
}
}
if($alreadyTranslatedLocales) {
if(!$tab->fieldByName('ExistingTransHeader')) {
$tab->push(new HeaderField(
'ExistingTransHeader',
_t('Translatable.EXISTING', 'Existing translations'),
3
));
if (!$tab->fieldByName('existingtrans')) {
$existingTransHTML = '<ul>';
if ($existingTranslations = $this->getTranslations()) {
foreach ($existingTranslations as $existingTranslation) {
if ($existingTranslation && $existingTranslation->hasMethod('CMSEditLink')) {
$existingTransHTML .= sprintf(
'<li><a href="%s">%s</a></li>',
Controller::join_links(
$existingTranslation->CMSEditLink(),
'?Locale=' . $existingTranslation->Locale
),
i18n::get_locale_name($existingTranslation->Locale)
);
}
}
}
$existingTransHTML .= '</ul>';
$tab->push(new LiteralField('existingtrans', $existingTransHTML));
}
$existingTransHTML .= '</ul>';
$tab->push(new LiteralField('existingtrans', $existingTransHTML));
}
}
}
}
}

function updateSettingsFields(&$fields) {
Expand Down