Thanks to this post, I was able to solve a problem that had no solution online: the ability to programmatically create a translation with all the fields of the original node. I scoured for solutions and found none, but thanks to your suggestion on iterating over fields, I was able to do it. Huge thank-you!

Here's the basic code that did it. I'm sure it needs tweaks as I'm skipping all non-fields except for path, but for now it's getting the job done!

        $node_trans = $node->addTranslation('en-au'); // sample using Australian English
        $node_trans->setTitle($node->getTitle());
        $node_trans_fields = $node->getTranslatableFields();
        foreach ($node_trans_fields as $name => $field) {
          if (substr($name, 0, 6) == 'field_' || in_array($name, ['body', 'path'])) {
            $node_trans->set($name, $field->getValue());
          }
        }
        try {
          $node->save();
        }
        catch (\Exception $error) {
          $add_status .= 'ERROR saving ';
        }
Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.

Restricted HTML

  • Zulässige HTML-Tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <pre> <ul type> <ol start type> <li> <dl> <dt> <dd> <h4 id> <h5 id> <h6 id>
  • HTML - Zeilenumbrüche und Absätze werden automatisch erzeugt.

Angebot innerhalb von 24 Stunden

Ob ein großes kommerzielles System, oder eine kleine Business Seite, wir schicken ein Angebot ab innerhalb von 24 Stunden nachdem Sie diese Taste drücken: Angebot anfordern