Set the product display's title and product entity's title based on some fields of the product display.
<?php
// define('BANNER_CT_NAME', 'banner');
/**
* Implement hook_node_presave()
**/
function the_aim_custom_node_presave($node) {
if($node->type === 'product_display') {
if($node->title === '<autogenerate-title>') {
// Set the title based on the value of the fields from the entity.
if(isset($node->field_brand)) {
$brands = field_get_items('node', $node, 'field_brand');
$brand = array_shift($brands);
}
if(isset($node->field_year)) {
$years = field_get_items('node', $node, 'field_year');
$year = array_shift($years);
}
if(isset($brand['name']) && isset($year['name'])) {
$node->title = $brand['name'] . ' ' . $year['name'];
return $node;
}
}
}
}
/**
* Implement hook_form_alter().
**/
function the_aim_custom_form_alter(&$form, &$form_state, $form_id) {
if($form_id === 'product_display_node_form') {
if(!isset($form_state['build_info']['args'][0]->nid)) {
$form['title']['#default_value'] = '<autogenerate-title>';
$form['title']['#type'] = 'hidden';
}
}
}
/**
* Implements hook_entity_info_alter().
*/
function the_aim_custom_entity_info_alter(&$entity_info) {
if (isset($entity_info['commerce_product'])) {
$entity_info['commerce_product']['inline entity form'] = array(
'controller' => 'CustomCommerceProductInlineEntityFormController',
);
}
}
class CustomCommerceProductInlineEntityFormController extends CommerceProductInlineEntityFormController {
/**
* Overrides CommerceProductInlineEntityFormController::save().
*
* Autogenerates the product title if specified, and then saves the product.
*/
public function save($entity, $context) {
// Remove the temporary message added in entityFormSubmit() so that
// Commerce AutoSKU can do its thing.
if (!empty($entity->_remove_sku)) {
$entity->sku = NULL;
unset($entity->_remove_sku);
}
// Generate the product title. Take the parent entity title as the base.
if ($this->settings['autogenerate_title']) {
$entity->title = entity_label($context['parent_entity_type'], $context['parent_entity']);
if($entity->title === '<autogenerate-title>') {
// Set the title based on the value of the fields from the entity.
if(isset($context['parent_entity']->field_brand)) {
$brands = field_get_items($context['parent_entity_type'], $context['parent_entity'], 'field_brand');
$brand = array_shift($brands);
}
if(isset($context['parent_entity']->field_year)) {
$years = field_get_items($context['parent_entity_type'], $context['parent_entity'], 'field_year');
$year = array_shift($years);
}
if(isset($brand['name']) && isset($year['name'])) {
$entity->title = $brand['name'] . ' ' . $year['name'];
}
}
$attributes = $this->attributes($entity->type);
if (!empty($attributes)) {
$wrapper = entity_metadata_wrapper('commerce_product', $entity);
$attribute_values = array();
foreach ($attributes as $field_name => $attribute) {
$attribute_label = $wrapper->{$field_name}->label();
if (!empty($attribute_label)) {
$attribute_values[] = $attribute_label;
}
}
if (!empty($attribute_values)) {
$entity->title .= ' (' . implode(', ', $attribute_values) . ')';
}
}
}
entity_save('commerce_product', $entity);
}
}