Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
< ? php
/**
* @ file
2013-06-25 19:16:20 +00:00
* The content translation administration forms .
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
*/
use Drupal\Core\Entity\EntityInterface ;
2013-10-04 07:55:32 +00:00
use Drupal\Core\Entity\Field\FieldDefinitionInterface ;
2013-05-25 20:12:45 +00:00
use Drupal\Core\Language\Language ;
2013-10-14 15:40:28 +00:00
use Drupal\field\Field as FieldService ;
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
2012-12-29 08:13:54 +00:00
/**
2013-02-19 06:57:04 +00:00
* Returns a form element to configure field synchronization .
2012-12-29 08:13:54 +00:00
*
2013-10-04 07:55:32 +00:00
* @ param \Drupal\Core\Entity\Field\FieldDefinitionInterface $field
* A field definition object .
2013-02-19 06:57:04 +00:00
*
* @ return array
* A form element to configure field synchronization .
*/
2013-10-04 07:55:32 +00:00
function content_translation_field_sync_widget ( FieldDefinitionInterface $field ) {
2013-02-19 06:57:04 +00:00
$element = array ();
2013-10-04 07:55:32 +00:00
$column_groups = $field -> getFieldSetting ( 'column_groups' );
if ( ! empty ( $column_groups ) && count ( $column_groups ) > 1 ) {
2013-02-19 06:57:04 +00:00
$options = array ();
$default = array ();
2013-10-04 07:55:32 +00:00
foreach ( $column_groups as $group => $info ) {
2013-02-19 06:57:04 +00:00
$options [ $group ] = $info [ 'label' ];
$default [ $group ] = ! empty ( $info [ 'translatable' ]) ? $group : FALSE ;
}
2013-03-10 06:18:48 +00:00
$settings = array ( 'dependent_selectors' => array ( 'instance[settings][translation_sync]' => array ( 'file' )));
2013-10-04 07:55:32 +00:00
$translation_sync = $field -> getFieldSetting ( 'translation_sync' );
2013-02-19 06:57:04 +00:00
$element = array (
'#type' => 'checkboxes' ,
'#title' => t ( 'Translatable elements' ),
'#options' => $options ,
2013-10-04 07:55:32 +00:00
'#default_value' => ! empty ( $translation_sync ) ? $translation_sync : $default ,
2013-03-10 06:18:48 +00:00
'#attached' => array (
2013-06-05 17:43:43 +00:00
'library' => array (
2013-06-25 19:16:20 +00:00
array ( 'content_translation' , 'drupal.content_translation.admin' ),
2013-06-05 17:43:43 +00:00
),
2013-03-10 06:18:48 +00:00
'js' => array (
2013-06-25 19:16:20 +00:00
array ( 'data' => array ( 'contentTranslationDependentOptions' => $settings ), 'type' => 'setting' ),
2013-03-10 06:18:48 +00:00
),
),
2013-02-19 06:57:04 +00:00
);
}
return $element ;
}
/**
* ( proxied ) Implements hook_form_FORM_ID_alter () .
2012-12-29 08:13:54 +00:00
*/
2013-06-25 19:16:20 +00:00
function _content_translation_form_language_content_settings_form_alter ( array & $form , array & $form_state ) {
2012-12-29 08:13:54 +00:00
// Inject into the content language settings the translation settings if the
// user has the required permission.
2013-06-25 19:16:20 +00:00
if ( ! user_access ( 'administer content translation' )) {
2012-12-29 08:13:54 +00:00
return ;
}
$default = $form [ 'entity_types' ][ '#default_value' ];
foreach ( $default as $entity_type => $enabled ) {
2013-06-25 19:16:20 +00:00
$default [ $entity_type ] = $enabled || content_translation_enabled ( $entity_type ) ? $entity_type : FALSE ;
2012-12-29 08:13:54 +00:00
}
$form [ 'entity_types' ][ '#default_value' ] = $default ;
2013-06-25 19:16:20 +00:00
$form [ '#attached' ][ 'library' ][] = array ( 'content_translation' , 'drupal.content_translation.admin' );
$form [ '#attached' ][ 'js' ][] = array ( 'data' => drupal_get_path ( 'module' , 'content_translation' ) . '/content_translation.admin.js' , 'type' => 'file' );
2012-12-29 08:13:54 +00:00
2013-03-10 06:18:48 +00:00
$dependent_options_settings = array ();
2013-10-14 15:40:28 +00:00
$entity_manager = Drupal :: entityManager ();
2012-12-29 08:13:54 +00:00
foreach ( $form [ '#labels' ] as $entity_type => $label ) {
2013-05-06 10:31:46 +00:00
$entity_info = entity_get_info ( $entity_type );
2013-01-23 17:46:47 +00:00
foreach ( entity_get_bundles ( $entity_type ) as $bundle => $bundle_info ) {
2012-12-29 08:13:54 +00:00
// Here we do not want the widget to be altered and hold also the "Enable
// translation" checkbox, which would be redundant. Hence we add this key
// to be able to skip alterations.
2013-06-25 19:16:20 +00:00
$form [ 'settings' ][ $entity_type ][ $bundle ][ 'settings' ][ 'language' ][ '#content_translation_skip_alter' ] = TRUE ;
2012-12-29 08:13:54 +00:00
2013-05-06 10:31:46 +00:00
// Only show the checkbox to enable translation if the bundles in the
// entity might have fields and if there are fields to translate.
if ( ! empty ( $entity_info [ 'fieldable' ])) {
2013-10-14 15:40:28 +00:00
$fields = $entity_manager -> getFieldDefinitions ( $entity_type , $bundle );
if ( $fields ) {
2013-05-06 10:31:46 +00:00
$form [ 'settings' ][ $entity_type ][ $bundle ][ 'translatable' ] = array (
'#type' => 'checkbox' ,
2013-06-25 19:16:20 +00:00
'#default_value' => content_translation_enabled ( $entity_type , $bundle ),
2013-05-06 10:31:46 +00:00
);
2013-10-14 15:40:28 +00:00
$field_settings = content_translation_get_config ( $entity_type , $bundle , 'fields' );
foreach ( $fields as $field_name => $definition ) {
$translatable = ! empty ( $field_settings [ $field_name ]);
// We special case Field API fields as they always natively support
// translation.
// @todo Remove this special casing as soon as configurable and
// base field definitions are "unified".
if ( ! empty ( $definition [ 'configurable' ]) && ( $field = FieldService :: fieldInfo () -> getField ( $entity_type , $field_name ))) {
$instance = FieldService :: fieldInfo () -> getInstance ( $entity_type , $bundle , $field_name );
$form [ 'settings' ][ $entity_type ][ $bundle ][ 'fields' ][ $field_name ] = array (
'#label' => $instance -> getFieldLabel (),
'#type' => 'checkbox' ,
'#default_value' => $translatable ,
);
$column_element = content_translation_field_sync_widget ( $instance );
if ( $column_element ) {
$form [ 'settings' ][ $entity_type ][ $bundle ][ 'columns' ][ $field_name ] = $column_element ;
// @todo This should not concern only files.
if ( isset ( $column_element [ '#options' ][ 'file' ])) {
$dependent_options_settings [ " settings[ { $entity_type } ][ { $bundle } ][columns][ { $field_name } ] " ] = array ( 'file' );
}
2013-05-06 10:31:46 +00:00
}
}
2013-10-14 15:40:28 +00:00
// Instead we need to rely on field definitions to determine whether
// fields support translation. Whether they are actually enabled is
// determined through our settings. As a consequence only fields
// that support translation can be enabled or disabled.
elseif ( isset ( $field_settings [ $field_name ]) || ! empty ( $definition [ 'translatable' ])) {
$form [ 'settings' ][ $entity_type ][ $bundle ][ 'fields' ][ $field_name ] = array (
'#label' => $definition [ 'label' ],
'#type' => 'checkbox' ,
'#default_value' => $translatable ,
);
}
2013-03-10 06:18:48 +00:00
}
2013-02-19 06:57:04 +00:00
}
2012-12-29 08:13:54 +00:00
}
}
}
2013-03-10 06:18:48 +00:00
$settings = array ( 'dependent_selectors' => $dependent_options_settings );
2013-06-25 19:16:20 +00:00
$form [ '#attached' ][ 'js' ][] = array ( 'data' => array ( 'contentTranslationDependentOptions' => $settings ), 'type' => 'setting' );
$form [ '#validate' ][] = 'content_translation_form_language_content_settings_validate' ;
$form [ '#submit' ][] = 'content_translation_form_language_content_settings_submit' ;
2012-12-29 08:13:54 +00:00
}
2013-02-19 06:57:04 +00:00
/**
* ( proxied ) Implements hook_preprocess_HOOK ();
*/
2013-06-25 19:16:20 +00:00
function _content_translation_preprocess_language_content_settings_table ( & $variables ) {
2013-02-19 06:57:04 +00:00
// Alter the 'build' variable injecting the translation settings if the user
// has the required permission.
2013-06-25 19:16:20 +00:00
if ( ! user_access ( 'administer content translation' )) {
2013-02-19 06:57:04 +00:00
return ;
}
$element = $variables [ 'element' ];
$build = & $variables [ 'build' ];
array_unshift ( $build [ '#header' ], array ( 'data' => t ( 'Translatable' ), 'class' => array ( 'translatable' )));
$rows = array ();
foreach ( element_children ( $element ) as $bundle ) {
$field_names = ! empty ( $element [ $bundle ][ 'fields' ]) ? element_children ( $element [ $bundle ][ 'fields' ]) : array ();
2013-05-06 10:31:46 +00:00
if ( ! empty ( $element [ $bundle ][ 'translatable' ])) {
$checkbox_id = $element [ $bundle ][ 'translatable' ][ '#id' ];
}
2013-02-19 06:57:04 +00:00
$rows [ $bundle ] = $build [ '#rows' ][ $bundle ];
2013-05-06 10:31:46 +00:00
if ( ! empty ( $element [ $bundle ][ 'translatable' ])) {
$translatable = array (
'data' => $element [ $bundle ][ 'translatable' ],
'class' => array ( 'translatable' ),
);
array_unshift ( $rows [ $bundle ][ 'data' ], $translatable );
2013-02-19 06:57:04 +00:00
2013-05-06 10:31:46 +00:00
$rows [ $bundle ][ 'data' ][ 1 ][ 'data' ][ '#prefix' ] = '<label for="' . $checkbox_id . '">' ;
}
else {
$translatable = array (
'class' => array ( 'untranslatable' ),
);
array_unshift ( $rows [ $bundle ][ 'data' ], $translatable );
}
2013-02-19 06:57:04 +00:00
foreach ( $field_names as $field_name ) {
$field_element = & $element [ $bundle ][ 'fields' ][ $field_name ];
$rows [] = array (
'data' => array (
array (
'data' => drupal_render ( $field_element ),
'class' => array ( 'translatable' ),
),
array (
'data' => array (
'#prefix' => '<label for="' . $field_element [ '#id' ] . '">' ,
'#suffix' => '</label>' ,
'bundle' => array (
2013-06-17 19:58:27 +00:00
'#prefix' => '<span class="visually-hidden">' ,
2013-02-19 06:57:04 +00:00
'#suffix' => '</span> ' ,
'#markup' => check_plain ( $element [ $bundle ][ 'settings' ][ '#label' ]),
),
'field' => array (
'#markup' => check_plain ( $field_element [ '#label' ]),
),
),
'class' => array ( 'field' ),
),
array (
'data' => '' ,
'class' => array ( 'operations' ),
),
),
'class' => array ( 'field-settings' ),
);
if ( ! empty ( $element [ $bundle ][ 'columns' ][ $field_name ])) {
$column_element = & $element [ $bundle ][ 'columns' ][ $field_name ];
foreach ( element_children ( $column_element ) as $key ) {
$column_label = $column_element [ $key ][ '#title' ];
unset ( $column_element [ $key ][ '#title' ]);
$rows [] = array (
'data' => array (
array (
'data' => drupal_render ( $column_element [ $key ]),
'class' => array ( 'translatable' ),
),
array (
'data' => array (
'#prefix' => '<label for="' . $column_element [ $key ][ '#id' ] . '">' ,
'#suffix' => '</label>' ,
'bundle' => array (
2013-06-17 19:58:27 +00:00
'#prefix' => '<span class="visually-hidden">' ,
2013-02-19 06:57:04 +00:00
'#suffix' => '</span> ' ,
'#markup' => check_plain ( $element [ $bundle ][ 'settings' ][ '#label' ]),
),
'field' => array (
2013-06-17 19:58:27 +00:00
'#prefix' => '<span class="visually-hidden">' ,
2013-02-19 06:57:04 +00:00
'#suffix' => '</span> ' ,
'#markup' => check_plain ( $field_element [ '#label' ]),
),
'columns' => array (
'#markup' => check_plain ( $column_label ),
),
),
'class' => array ( 'column' ),
),
array (
'data' => '' ,
'class' => array ( 'operations' ),
),
),
'class' => array ( 'column-settings' ),
);
}
}
}
}
$build [ '#rows' ] = $rows ;
}
2012-12-29 08:13:54 +00:00
/**
2013-06-25 19:16:20 +00:00
* Form validation handler for content_translation_admin_settings_form () .
2012-12-29 08:13:54 +00:00
*
2013-06-25 19:16:20 +00:00
* @ see content_translation_admin_settings_form_submit ()
2012-12-29 08:13:54 +00:00
*/
2013-06-25 19:16:20 +00:00
function content_translation_form_language_content_settings_validate ( array $form , array & $form_state ) {
2012-12-29 08:13:54 +00:00
$settings = & $form_state [ 'values' ][ 'settings' ];
foreach ( $settings as $entity_type => $entity_settings ) {
foreach ( $entity_settings as $bundle => $bundle_settings ) {
if ( ! empty ( $bundle_settings [ 'translatable' ])) {
$name = " settings][ $entity_type ][ $bundle ][translatable " ;
$translatable_fields = isset ( $settings [ $entity_type ][ $bundle ][ 'fields' ]) ? array_filter ( $settings [ $entity_type ][ $bundle ][ 'fields' ]) : FALSE ;
if ( empty ( $translatable_fields )) {
$t_args = array ( '%bundle' => $form [ 'settings' ][ $entity_type ][ $bundle ][ 'settings' ][ '#label' ]);
form_set_error ( $name , t ( 'At least one field needs to be translatable to enable %bundle for translation.' , $t_args ));
}
$values = $bundle_settings [ 'settings' ][ 'language' ];
2013-01-16 01:11:30 +00:00
if ( language_is_locked ( $values [ 'langcode' ]) && empty ( $values [ 'language_show' ])) {
2013-05-25 20:12:45 +00:00
foreach ( language_list ( Language :: STATE_LOCKED ) as $language ) {
2012-12-29 08:13:54 +00:00
$locked_languages [] = $language -> name ;
}
form_set_error ( $name , t ( 'Translation is not supported if language is always one of: @locked_languages' , array ( '@locked_languages' => implode ( ', ' , $locked_languages ))));
}
}
}
}
}
/**
2013-06-25 19:16:20 +00:00
* Form submission handler for content_translation_admin_settings_form () .
2012-12-29 08:13:54 +00:00
*
2013-06-25 19:16:20 +00:00
* @ see content_translation_admin_settings_form_validate ()
2012-12-29 08:13:54 +00:00
*/
2013-06-25 19:16:20 +00:00
function content_translation_form_language_content_settings_submit ( array $form , array & $form_state ) {
2012-12-29 08:13:54 +00:00
$entity_types = $form_state [ 'values' ][ 'entity_types' ];
$settings = & $form_state [ 'values' ][ 'settings' ];
// If an entity type is not translatable all its bundles and fields must be
// marked as non-translatable. Similarly, if a bundle is made non-translatable
// all of its fields will be not translatable.
foreach ( $settings as $entity_type => & $entity_settings ) {
foreach ( $entity_settings as $bundle => & $bundle_settings ) {
2013-05-06 10:31:46 +00:00
if ( ! empty ( $bundle_settings [ 'translatable' ])) {
$bundle_settings [ 'translatable' ] = $bundle_settings [ 'translatable' ] && $entity_types [ $entity_type ];
}
2012-12-29 08:13:54 +00:00
if ( ! empty ( $bundle_settings [ 'fields' ])) {
foreach ( $bundle_settings [ 'fields' ] as $field_name => $translatable ) {
$bundle_settings [ 'fields' ][ $field_name ] = $translatable && $bundle_settings [ 'translatable' ];
2013-02-19 06:57:04 +00:00
// If we have column settings and no column is translatable, no point
// in making the field translatable.
if ( isset ( $bundle_settings [ 'columns' ][ $field_name ]) && ! array_filter ( $bundle_settings [ 'columns' ][ $field_name ])) {
$bundle_settings [ 'fields' ][ $field_name ] = FALSE ;
}
2012-12-29 08:13:54 +00:00
}
}
}
}
2013-06-25 19:16:20 +00:00
_content_translation_update_field_translatability ( $settings );
2012-12-29 08:13:54 +00:00
drupal_set_message ( t ( 'Settings successfully updated.' ));
}
/**
2013-06-25 19:16:20 +00:00
* Stores content translation settings .
2012-12-29 08:13:54 +00:00
*
* @ param array $settings
* An associative array of settings keyed by entity type and bundle . At bundle
* level the following keys are available :
* - translatable : The bundle translatability status , which is a bool .
* - settings : An array of language configuration settings as defined by
* language_save_default_configuration () .
* - fields : An associative array with field names as keys and a boolean as
* value , indicating field translatability .
*
* @ todo Remove this migration entirely once the Field API is converted to the
* Entity Field API .
*/
2013-06-25 19:16:20 +00:00
function _content_translation_update_field_translatability ( $settings ) {
2012-12-29 08:13:54 +00:00
$fields = array ();
foreach ( $settings as $entity_type => $entity_settings ) {
foreach ( $entity_settings as $bundle => $bundle_settings ) {
// Collapse field settings since here we have per instance settings, but
// translatability has per-field scope. We assume that all the field
// instances have the same value.
if ( ! empty ( $bundle_settings [ 'fields' ])) {
foreach ( $bundle_settings [ 'fields' ] as $field_name => $translatable ) {
// If a field is enabled for translation for at least one instance we
// need to mark it as translatable.
2013-10-14 15:40:28 +00:00
if ( FieldService :: fieldInfo () -> getField ( $entity_type , $field_name )) {
$fields [ $entity_type ][ $field_name ] = $translatable || ! empty ( $fields [ $entity_type ][ $field_name ]);
}
2012-12-29 08:13:54 +00:00
}
}
}
}
$operations = array ();
2013-09-01 06:20:08 +00:00
foreach ( $fields as $entity_type => $entity_type_fields ) {
foreach ( $entity_type_fields as $field_name => $translatable ) {
$field = field_info_field ( $entity_type , $field_name );
2013-10-04 07:55:32 +00:00
if ( $field -> isFieldTranslatable () != $translatable ) {
2013-09-01 06:20:08 +00:00
// If a field is untranslatable, it can have no data except under
// Language::LANGCODE_NOT_SPECIFIED. Thus we need a field to be translatable before
// we convert data to the entity language. Conversely we need to switch
// data back to Language::LANGCODE_NOT_SPECIFIED before making a field
// untranslatable lest we lose information.
$field_operations = array (
array ( 'content_translation_translatable_switch' , array ( $translatable , $entity_type , $field_name )),
);
if ( $field -> hasData ()) {
$field_operations [] = array ( 'content_translation_translatable_batch' , array ( $translatable , $field_name ));
$field_operations = $translatable ? $field_operations : array_reverse ( $field_operations );
}
$operations = array_merge ( $operations , $field_operations );
2012-12-29 08:13:54 +00:00
}
}
}
// As last operation store the submitted settings.
2013-06-25 19:16:20 +00:00
$operations [] = array ( 'content_translation_save_settings' , array ( $settings ));
2012-12-29 08:13:54 +00:00
$batch = array (
'title' => t ( 'Updating translatability for the selected fields' ),
'operations' => $operations ,
2013-06-25 19:16:20 +00:00
'finished' => 'content_translation_translatable_batch_done' ,
'file' => drupal_get_path ( 'module' , 'content_translation' ) . '/content_translation.admin.inc' ,
2012-12-29 08:13:54 +00:00
);
batch_set ( $batch );
}
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
/**
* Toggles translatability of the given field .
*
* This is called from a batch operation , but should only run once per field .
*
* @ param bool $translatable
* Indicator of whether the field should be made translatable ( TRUE ) or
* untranslatble ( FALSE ) .
2013-09-01 06:20:08 +00:00
* @ param string $entity_type
* Field entity type .
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
* @ param string $field_name
* Field machine name .
*/
2013-09-01 06:20:08 +00:00
function content_translation_translatable_switch ( $translatable , $entity_type , $field_name ) {
$field = field_info_field ( $entity_type , $field_name );
2013-10-04 07:55:32 +00:00
if ( $field -> isFieldTranslatable () !== $translatable ) {
$field -> translatable = $translatable ;
2013-06-18 10:43:19 +00:00
$field -> save ();
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
}
}
/**
2013-05-25 20:12:45 +00:00
* Batch callback : Converts field data to or from Language :: LANGCODE_NOT_SPECIFIED .
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
*
* @ param bool $translatable
* Indicator of whether the field should be made translatable ( TRUE ) or
* untranslatble ( FALSE ) .
* @ param string $field_name
* Field machine name .
*/
2013-06-25 19:16:20 +00:00
function content_translation_translatable_batch ( $translatable , $field_name , & $context ) {
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
// Determine the entity types to act on.
2012-12-29 08:13:54 +00:00
$entity_types = array ();
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
foreach ( field_info_instances () as $entity_type => $info ) {
foreach ( $info as $bundle => $instances ) {
foreach ( $instances as $instance_field_name => $instance ) {
if ( $instance_field_name == $field_name ) {
$entity_types [] = $entity_type ;
break 2 ;
}
}
}
}
if ( empty ( $context [ 'sandbox' ])) {
$context [ 'sandbox' ][ 'progress' ] = 0 ;
$context [ 'sandbox' ][ 'max' ] = 0 ;
foreach ( $entity_types as $entity_type ) {
2013-09-01 06:20:08 +00:00
$field = field_info_field ( $entity_type , $field_name );
2013-10-04 07:55:32 +00:00
$columns = $field -> getColumns ();
$column = isset ( $columns [ 'value' ]) ? 'value' : key ( $columns );
2013-09-01 06:20:08 +00:00
$query_field = " $field_name . $column " ;
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
// How many entities will need processing?
2013-09-16 03:58:06 +00:00
$query = \Drupal :: entityQuery ( $entity_type );
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
$count = $query
2012-12-29 08:13:54 +00:00
-> exists ( $query_field )
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
-> count ()
-> execute ();
$context [ 'sandbox' ][ 'max' ] += $count ;
$context [ 'sandbox' ][ 'progress_entity_type' ][ $entity_type ] = 0 ;
$context [ 'sandbox' ][ 'max_entity_type' ][ $entity_type ] = $count ;
}
if ( $context [ 'sandbox' ][ 'max' ] === 0 ) {
// Nothing to do.
$context [ 'finished' ] = 1 ;
return ;
}
}
foreach ( $entity_types as $entity_type ) {
if ( $context [ 'sandbox' ][ 'max_entity_type' ][ $entity_type ] === 0 ) {
continue ;
}
$info = entity_get_info ( $entity_type );
$offset = $context [ 'sandbox' ][ 'progress_entity_type' ][ $entity_type ];
2013-09-16 03:58:06 +00:00
$query = \Drupal :: entityQuery ( $entity_type );
2013-09-01 06:20:08 +00:00
$field = field_info_field ( $entity_type , $field_name );
2013-10-04 07:55:32 +00:00
$columns = $field -> getColumns ();
$column = isset ( $columns [ 'value' ]) ? 'value' : key ( $columns );
2013-09-01 06:20:08 +00:00
$query_field = " $field_name . $column " ;
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
$result = $query
2012-12-29 08:13:54 +00:00
-> exists ( $query_field )
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
-> sort ( $info [ 'entity_keys' ][ 'id' ])
-> range ( $offset , 10 )
-> execute ();
foreach ( entity_load_multiple ( $entity_type , $result ) as $id => $entity ) {
$context [ 'sandbox' ][ 'max_entity_type' ][ $entity_type ] -= count ( $result );
$context [ 'sandbox' ][ 'progress_entity_type' ][ $entity_type ] ++ ;
$context [ 'sandbox' ][ 'progress' ] ++ ;
2013-06-29 10:56:53 +00:00
$langcode = $entity -> language () -> id ;
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
// Skip process for language neutral entities.
2013-05-25 20:12:45 +00:00
if ( $langcode == Language :: LANGCODE_NOT_SPECIFIED ) {
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
continue ;
}
// We need a two-step approach while updating field translations: given
// that field-specific update functions might rely on the stored values to
2013-09-09 21:23:15 +00:00
// perform their processing first we need to store the new translations
// and only after we can remove the old ones. Otherwise we might have data
// loss, since the removal of the old translations might occur before the
// new ones are stored.
2013-05-25 20:12:45 +00:00
if ( $translatable && isset ( $entity -> { $field_name }[ Language :: LANGCODE_NOT_SPECIFIED ])) {
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
// If the field is being switched to translatable and has data for
2013-05-25 20:12:45 +00:00
// Language::LANGCODE_NOT_SPECIFIED then we need to move the data to the right
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
// language.
2013-05-25 20:12:45 +00:00
$entity -> { $field_name }[ $langcode ] = $entity -> { $field_name }[ Language :: LANGCODE_NOT_SPECIFIED ];
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
// Store the original value.
2013-06-25 19:16:20 +00:00
_content_translation_update_field ( $entity_type , $entity , $field_name );
2013-05-25 20:12:45 +00:00
$entity -> { $field_name }[ Language :: LANGCODE_NOT_SPECIFIED ] = array ();
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
// Remove the language neutral value.
2013-06-25 19:16:20 +00:00
_content_translation_update_field ( $entity_type , $entity , $field_name );
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
}
elseif ( ! $translatable && isset ( $entity -> { $field_name }[ $langcode ])) {
// The field has been marked untranslatable and has data in the entity
2013-05-25 20:12:45 +00:00
// language: we need to move it to Language::LANGCODE_NOT_SPECIFIED and drop the
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
// other translations.
2013-05-25 20:12:45 +00:00
$entity -> { $field_name }[ Language :: LANGCODE_NOT_SPECIFIED ] = $entity -> { $field_name }[ $langcode ];
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
// Store the original value.
2013-06-25 19:16:20 +00:00
_content_translation_update_field ( $entity_type , $entity , $field_name );
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
// Remove translations.
foreach ( $entity -> { $field_name } as $langcode => $items ) {
2013-05-25 20:12:45 +00:00
if ( $langcode != Language :: LANGCODE_NOT_SPECIFIED ) {
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
$entity -> { $field_name }[ $langcode ] = array ();
}
}
2013-06-25 19:16:20 +00:00
_content_translation_update_field ( $entity_type , $entity , $field_name );
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
}
else {
// No need to save unchanged entities.
continue ;
}
}
}
$context [ 'finished' ] = $context [ 'sandbox' ][ 'progress' ] / $context [ 'sandbox' ][ 'max' ];
}
/**
* Stores the given field translations .
*/
2013-06-25 19:16:20 +00:00
function _content_translation_update_field ( $entity_type , EntityInterface $entity , $field_name ) {
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
$empty = 0 ;
2013-06-25 10:27:47 +00:00
$translations = $entity -> getTranslationLanguages ();
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
// Ensure that we are trying to store only valid data.
2013-06-25 10:27:47 +00:00
foreach ( array_keys ( $translations ) as $langcode ) {
$items = $entity -> getTranslation ( $langcode ) -> get ( $field_name );
$items -> filterEmptyValues ();
$empty += $items -> isEmpty ();
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
}
// Save the field value only if there is at least one item available,
// otherwise any stored empty field value would be deleted. If this happens
// the range queries would be messed up.
2013-06-25 10:27:47 +00:00
if ( $empty < count ( $translations )) {
$entity -> save ();
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
}
}
/**
* Batch finished callback : Checks the exit status of the batch operation .
*/
2013-06-25 19:16:20 +00:00
function content_translation_translatable_batch_done ( $success , $results , $operations ) {
Issue #1188388 by plach, peximo, YesCT | Gábor Hojtsy, fago, webchick, Bojhan, podarok, cosmicdreams, Berdir, aspilicious, bforchhammer, penyaskito: Added Entity translation UI in core.
2012-11-04 02:38:49 +00:00
if ( $success ) {
drupal_set_message ( t ( " Successfully changed field translation setting. " ));
}
else {
// @todo: Do something about this case.
drupal_set_message ( t ( " Something went wrong while processing data. Some nodes may appear to have lost fields. " ), 'error' );
}
}