public function synchronize(NodeInterface $node, Context $context, $dirtyAllowed = false) { /* @var $node FieldInstanceNode */ $entityType = $node->getEntityType(); $bundle = $node->getBundle(); $fieldName = $node->getFieldName(); $existing = field_info_instance($entityType, $fieldName, $bundle); $field = field_info_field($fieldName); $default = array('entity_type' => $entityType, 'bundle' => $bundle, 'field_name' => $fieldName); $object = $node->getValue(); if (!is_array($object)) { $object = array(); } if (empty($object['label'])) { // Field data 'label' key is not part of the Drupal signature // but this module will inject it anyway and should be persisted // along the field 'data' key in database if (empty($field['label'])) { if ($label = $this->findFieldLabel($fieldName)) { $default['label'] = $label; } } else { $default['label'] = $field['label']; } } // This is a forced default from this module: never display new // fields without being explicitely told to $instance = $default + $object + array('display' => array('default' => array('type' => 'hidden'))); // Dynamically determine weight using position relative to parent node // @todo Find a quicker way if ($node->hasParent()) { $instance['weight'] = $node->getParent()->getChildPosition($node); } // Propagate defaults set at the field level if (!empty($field['instance'])) { foreach ($field['instance'] as $key => $value) { if (!isset($instance[$key])) { $instance[$key] = $value; } } } // Even thought this is not mandatory few modules such as the 'image' // module will attempt to access this attribute, without carying about // the field_update_instance() method documentation if (!isset($instance['settings'])) { $instance['settings'] = array(); } // Deal with widget if (!isset($instance['widget'])) { if (isset($field['widget'])) { $instance['widget'] = $field['widget']; } } if ($existing) { $this->alter(self::HOOK_UPDATE, $node, $instance); field_update_instance($instance); } else { $this->alter(self::HOOK_INSERT, $node, $instance); field_create_instance($instance); } }