Example #1
0
 /**
  * {@inheritdoc}
  */
 public function getExistingObject(NodeInterface $node, Context $context)
 {
     $existing = menu_load($node->getName());
     if (!$existing) {
         $context->logCritical(sprintf("%s: does not exists", $node->getPath()));
     }
     return array_intersect_key($existing, self::$defaults) + self::$defaults;
 }
 public function getExistingObject(NodeInterface $node, Context $context)
 {
     /* @var $node FieldInstanceNode */
     $existing = field_info_instance($node->getEntityType(), $node->getName(), $node->getBundle());
     if (!$existing) {
         $context->logCritical(sprintf("%s: does not exists", $node->getPath()));
     }
     return $existing;
 }
 public function execute(Node $node, Context $context)
 {
     $sorted = array();
     $orphans = array();
     $path = $node->getPath();
     foreach ($node->getChildren() as $key => $child) {
         if ($child->hasChild('inherit')) {
             $parent = $child->getChild('inherit')->getValue();
             if (!is_string($parent)) {
                 $context->logCritical(sprintf("%s: %s: malformed inherit directive", $path, $key));
             }
             if (!$node->hasChild($parent)) {
                 $context->logCritical(sprintf("%s: %s: cannot inherit from non existing: %s", $path, $key, $parent));
             }
             if ($key === $parent) {
                 $context->logCritical(sprintf("%s: %s: cannot inherit from itself", $path, $key));
             }
             $child->removeChild('inherit');
             $orphans[$key] = $parent;
         } else {
             $sorted[$key] = array();
         }
     }
     while (!empty($orphans)) {
         $count = count($orphans);
         foreach ($orphans as $key => $parent) {
             if (isset($sorted[$parent])) {
                 $sorted[$parent][] = $key;
                 $sorted[$key] = array();
                 unset($orphans[$key]);
             }
         }
         if (count($orphans) === $count) {
             $context->logCritical(sprintf("%s: circular dependency detected", $path));
         }
     }
     foreach (array_filter($sorted) as $parent => $children) {
         foreach ($children as $name) {
             $node->getChild($name)->setAttribute('inherits', $node->getChild($parent)->getPath());
         }
     }
 }
 /**
  * Return the filter name depending on the tree structure
  *
  * @param NodeInterface $node
  * @param Context $context
  * @return array|mixed|string
  */
 protected function getFormatName(NodeInterface $node, Context $context)
 {
     if ($node->hasChild('name')) {
         $value = $node->getChild('name')->getValue();
         if (!is_string($value)) {
             $context->logCritical(sprintf("%s: name attribute is not a string", $node->getPath()));
         }
         return $value;
     }
     return $node->getName();
 }
 public function synchronize(NodeInterface $node, Context $context, $dirtyAllowed = false)
 {
     /* @var $node FieldNode */
     $object = $node->getValue();
     if (!is_array($object)) {
         $object = array();
     }
     if (!isset($object['type'])) {
         $context->logCritical(sprintf("%s: has no type", $node->getPath()));
     }
     $name = $node->getName();
     $type = $object['type'];
     $typeInfo = field_info_field_types($type);
     if (empty($typeInfo)) {
         $context->logCritical(sprintf("%s: type %s does not exist", $node->getPath(), $type));
     }
     if ($this->exists($node, $context)) {
         $existing = $this->getExistingObject($node, $context);
     } else {
         $existing = null;
     }
     if (array_key_exists('settings', $object) && !is_array($object['settings'])) {
         $context->log(sprintf("%s: no settings provided, defaulting with empty array", $node->getPath()));
         $object['settings'] = array();
     }
     $object['field_name'] = $name;
     if (empty($object['cardinality'])) {
         $object['cardinality'] = 1;
     }
     // Consistency check, prior to do anything, ensure the database tables
     // are not already there, this happens, sometimes
     if (!$existing) {
         if (empty($object['storage']) || 'field_sql_storage' === $object['storage']['type']) {
             // Prevents warning on unspecified key
             if (!isset($object['deleted'])) {
                 $object['deleted'] = false;
             }
             $tables = [_field_sql_storage_tablename($object), _field_sql_storage_revision_tablename($object)];
             foreach ($tables as $table) {
                 if (db_table_exists($table)) {
                     $context->logDataloss(sprintf("%s: %s: table already exists prior to creating field", $node->getPath(), $table));
                     // If code has not broken here, then go for deletion
                     db_drop_table($table);
                 }
             }
         }
     }
     if ($existing) {
         $doDelete = false;
         $eType = $existing['type'];
         // Ensure the cardinality change if any is safe to proceed with
         $cardinality = $object['cardinality'] - $existing['cardinality'];
         if (0 !== $cardinality) {
             if (0 < $cardinality || -1 == $object['cardinality']) {
                 $context->log(sprintf("%s: safe cardinality change", $node->getPath()));
             } else {
                 // @todo Ensure there is data we can save in field
                 if (false) {
                     $context->log(sprintf("%s: safe cardinality change due to data shape", $node->getPath()));
                 } else {
                     $context->logDataloss(sprintf("%s: unsafe cardinality change", $node->getPath()));
                 }
             }
         }
         if ($type !== $eType) {
             $doDelete = true;
             $instances = $this->getInstances($name);
             if (empty($instances)) {
                 $context->logWarning(sprintf("%s: type change (%s -> %s): no instances", $node->getPath(), $type, $eType));
             } else {
                 // @todo Ensure there is data if there is instances
                 if (false) {
                     $context->logWarning(sprintf("%s: type change (%s -> %s): existing instances are empty", $node->getPath(), $type, $eType));
                 } else {
                     // @todo Safe should ensure schema is the same
                     if (false) {
                         $context->logWarning(sprintf("%s: type change (%s -> %s): field schema is the same", $node->getPath(), $type, $eType));
                     } else {
                         $context->logDataloss(sprintf("%s: type change (%s -> %s): data loss detected", $node->getPath(), $type, $eType));
                     }
                 }
             }
         }
         if ($doDelete) {
             $this->deleteExistingObject($node, $context);
             field_create_field($object);
             // @todo Recreate instances
         } else {
             field_update_field($object);
         }
     } else {
         field_create_field($object);
     }
 }