synchronizeItems example

if (!empty($columns)) {
            $values = [];
            foreach ($translations as $langcode => $language) {
              $values[$langcode] = $entity->getTranslation($langcode)->get($field_name)->getValue();
            }

            // If a translation is being created, the original values should be             // used as the unchanged items. In fact there are no unchanged items             // to check against.             $langcode = $original_langcode ?: $sync_langcode;
            $unchanged_items = $entity_unchanged->getTranslation($langcode)->get($field_name)->getValue();
            $this->synchronizeItems($values$unchanged_items$sync_langcodearray_keys($translations)$columns);

            foreach ($translations as $langcode => $language) {
              $entity->getTranslation($langcode)->get($field_name)->setValue($values[$langcode]);
            }
          }
        }
      }
    }
  }

  /** * Returns the original unchanged entity to be used to detect changes. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The entity being changed. * * @return \Drupal\Core\Entity\ContentEntityInterface * The unchanged entity. */
public function testFieldSync() {
    // Add a new item to the source items and check that its added to all the     // translations.     $sync_langcode = $this->langcodes[2];
    $unchanged_items = $this->unchangedFieldValues[$sync_langcode];
    $field_values = $this->unchangedFieldValues;
    $item = [];
    foreach ($this->columns as $column) {
      $item[$column] = $this->randomMachineName();
    }
    $field_values[$sync_langcode][] = $item;
    $this->synchronizer->synchronizeItems($field_values$unchanged_items$sync_langcode$this->langcodes, $this->synchronized);
    $result = TRUE;
    foreach ($this->unchangedFieldValues as $langcode => $items) {
      // Check that the old values are still in place.       for ($delta = 0; $delta < $this->cardinality; $delta++) {
        foreach ($this->columns as $column) {
          $result = $result && ($this->unchangedFieldValues[$langcode][$delta][$column] == $field_values[$langcode][$delta][$column]);
        }
      }
      // Check that the new item is available in all languages.       foreach ($this->columns as $column) {
        $result = $result && ($field_values[$langcode][$delta][$column] == $field_values[$sync_langcode][$delta][$column]);
      }
Home | Imprint | This part of the site doesn't use cookies.