Skip to main content
deleted 2 characters in body
Source Link
user72672
user72672
/**
 * Implements hook_node_update().
 */
function MY_MODULE_node_update(\Drupal\node\NodeInterface $node) {
  $node =foreach $form_state->getFormObject()$node->getEntity>getFieldDefinitions();
 as $definition) {
  foreach  if ($node!in_array($definition->getFieldDefinitions>getType(), as['entity_reference', $definition'entity_reference_revisions'])) {
      continue;
    }
    if (!$definition instanceof \Drupal\field\Entity\FieldConfig->getSetting('target_type')) {
      continue;
    }
    if ($definition->getSettings>getSetting('target_type')['target_type'] == 'media') {
        $media = $node->{$definition->getName()}->entity;
        if ($media) {
          $media->groups->target_id = $node->groups;>groups->target_id;
          $media->save();
        }
      }
      if ($definition->getSettings>getSetting('target_type')['target_type'] == 'paragraph') {
        $paragraph = $node->{$definition->getName()}->entity;
        if ($paragraph) {
          ph_content_update_media_itemsMY_MODULE_update_media_items($paragraph, $node->groups);
        }
      }
    }
  }
}

/**
 * Set groups on all media items referenced in a paragraph.
 *
 * @param $paragraph
 *   The paragraph.
 * @param $groups
 *   The groups to set.
 */
function MY_MODULE_update_media_items($paragraph, $groups) {
  foreach ($paragraph->getFieldDefinitions() as $definition) {
    if (!in_array($definition->getType(), instanceof['entity_reference', \Drupal\field\Entity\FieldConfig'entity_reference_revisions'])) {
      continue;
    }
    if (!$definition->getSettings>getSetting('target_type')['target_type']) {
      continue;
    }
    if ($definition->getSetting('target_type') == 'media') {
        $media = $paragraph->{$definition->getName()}->entity;
        if ($media) {
          $media->groups->target_id = $groups;$groups->target_id;
          $media->save();
        }
      }
      if ($definition->getSettings>getSetting('target_type')['target_type'] == 'paragraph') {
        $paragraph = $paragraph->{$definition->getName()}->entity;
        if ($paragraph) {
          ph_content_update_media_itemsMY_MODULE_update_media_items($paragraph, $groups);
        }
      }
    }
  }
}
/**
 * Implements hook_node_update().
 */
function MY_MODULE_node_update(\Drupal\node\NodeInterface $node) {
  $node = $form_state->getFormObject()->getEntity();

  foreach ($node->getFieldDefinitions() as $definition) {
    if ($definition instanceof \Drupal\field\Entity\FieldConfig) {
      if ($definition->getSettings()['target_type'] == 'media') {
        $media = $node->{$definition->getName()}->entity;
        if ($media) {
          $media->groups = $node->groups;
          $media->save();
        }
      }
      if ($definition->getSettings()['target_type'] == 'paragraph') {
        $paragraph = $node->{$definition->getName()}->entity;
        if ($paragraph) {
          ph_content_update_media_items($paragraph, $node->groups);
        }
      }
    }
  }
}

/**
 * Set groups on all media items referenced in a paragraph.
 *
 * @param $paragraph
 *   The paragraph.
 * @param $groups
 *   The groups to set.
 */
function MY_MODULE_update_media_items($paragraph, $groups) {
  foreach ($paragraph->getFieldDefinitions() as $definition) {
    if ($definition instanceof \Drupal\field\Entity\FieldConfig) {
      if ($definition->getSettings()['target_type'] == 'media') {
        $media = $paragraph->{$definition->getName()}->entity;
        if ($media) {
          $media->groups = $groups;
          $media->save();
        }
      }
      if ($definition->getSettings()['target_type'] == 'paragraph') {
        $paragraph = $paragraph->{$definition->getName()}->entity;
        if ($paragraph) {
          ph_content_update_media_items($paragraph, $groups);
        }
      }
    }
  }
}
/**
 * Implements hook_node_update().
 */
function MY_MODULE_node_update(\Drupal\node\NodeInterface $node) {
  foreach ($node->getFieldDefinitions() as $definition) {
    if (!in_array($definition->getType(), ['entity_reference', 'entity_reference_revisions'])) {
      continue;
    }
    if (!$definition->getSetting('target_type')) {
      continue;
    }
    if ($definition->getSetting('target_type') == 'media') {
      $media = $node->{$definition->getName()}->entity;
      if ($media) {
        $media->groups->target_id = $node->groups->target_id;
        $media->save();
      }
    }
    if ($definition->getSetting('target_type') == 'paragraph') {
      $paragraph = $node->{$definition->getName()}->entity;
      if ($paragraph) {
        MY_MODULE_update_media_items($paragraph, $node->groups);
      }
    }
  }
}

/**
 * Set groups on all media items referenced in a paragraph.
 *
 * @param $paragraph
 *   The paragraph.
 * @param $groups
 *   The groups to set.
 */
function MY_MODULE_update_media_items($paragraph, $groups) {
  foreach ($paragraph->getFieldDefinitions() as $definition) {
    if (!in_array($definition->getType(), ['entity_reference', 'entity_reference_revisions'])) {
      continue;
    }
    if (!$definition->getSetting('target_type')) {
      continue;
    }
    if ($definition->getSetting('target_type') == 'media') {
      $media = $paragraph->{$definition->getName()}->entity;
      if ($media) {
        $media->groups->target_id = $groups->target_id;
        $media->save();
      }
    }
    if ($definition->getSetting('target_type') == 'paragraph') {
      $paragraph = $paragraph->{$definition->getName()}->entity;
      if ($paragraph) {
        MY_MODULE_update_media_items($paragraph, $groups);
      }
    }
  }
}
deleted 36 characters in body
Source Link
user72672
user72672

With the help from @Clive I found a solution:

/**
 * Custom form submission for nodeImplements formhook_node_update().
 */
function ph_content_node_form_submitMY_MODULE_node_update(array $form, \Drupal\Core\Form\FormStateInterface\Drupal\node\NodeInterface $form_state$node) {
  $node = $form_state->getFormObject()->getEntity();

  foreach ($node->getFieldDefinitions() as $definition) {
    if ($definition instanceof \Drupal\field\Entity\FieldConfig) {
      if ($definition->getSettings()['target_type'] == 'media') {
        $media = $node->{$definition->getName()}->entity;
        if ($media) {
          $media->groups = $node->groups;
          $media->save();
        }
      }
      if ($definition->getSettings()['target_type'] == 'paragraph') {
        $paragraph = $node->{$definition->getName()}->entity;
        if ($paragraph) {
          ph_content_update_media_items($paragraph, $node->groups);
        }
      }
    }
  }
}

/**
 * Set groups on all media items referenced in a paragraph.
 *
 * @param $paragraph
 *   The paragraph.
 * @param $groups
 *   The groups to set.
 */
function ph_content_update_media_itemsMY_MODULE_update_media_items($paragraph, $groups) {
  foreach ($paragraph->getFieldDefinitions() as $definition) {
    if ($definition instanceof \Drupal\field\Entity\FieldConfig) {
      if ($definition->getSettings()['target_type'] == 'media') {
        $media = $paragraph->{$definition->getName()}->entity;
        if ($media) {
          $media->groups = $groups;
          $media->save();
        }
      }
      if ($definition->getSettings()['target_type'] == 'paragraph') {
        $paragraph = $paragraph->{$definition->getName()}->entity;
        if ($paragraph) {
          ph_content_update_media_items($paragraph, $groups);
        }
      }
    }
  }
}

With the help from @Clive I found a solution:

/**
 * Custom form submission for node form.
 */
function ph_content_node_form_submit(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $node = $form_state->getFormObject()->getEntity();

  foreach ($node->getFieldDefinitions() as $definition) {
    if ($definition instanceof \Drupal\field\Entity\FieldConfig) {
      if ($definition->getSettings()['target_type'] == 'media') {
        $media = $node->{$definition->getName()}->entity;
        if ($media) {
          $media->groups = $node->groups;
          $media->save();
        }
      }
      if ($definition->getSettings()['target_type'] == 'paragraph') {
        $paragraph = $node->{$definition->getName()}->entity;
        if ($paragraph) {
          ph_content_update_media_items($paragraph, $node->groups);
        }
      }
    }
  }
}

/**
 * Set groups on all media items referenced in a paragraph.
 *
 * @param $paragraph
 *   The paragraph.
 * @param $groups
 *   The groups to set.
 */
function ph_content_update_media_items($paragraph, $groups) {
  foreach ($paragraph->getFieldDefinitions() as $definition) {
    if ($definition instanceof \Drupal\field\Entity\FieldConfig) {
      if ($definition->getSettings()['target_type'] == 'media') {
        $media = $paragraph->{$definition->getName()}->entity;
        if ($media) {
          $media->groups = $groups;
          $media->save();
        }
      }
      if ($definition->getSettings()['target_type'] == 'paragraph') {
        $paragraph = $paragraph->{$definition->getName()}->entity;
        if ($paragraph) {
          ph_content_update_media_items($paragraph, $groups);
        }
      }
    }
  }
}

With the help from @Clive I found a solution:

/**
 * Implements hook_node_update().
 */
function MY_MODULE_node_update(\Drupal\node\NodeInterface $node) {
  $node = $form_state->getFormObject()->getEntity();

  foreach ($node->getFieldDefinitions() as $definition) {
    if ($definition instanceof \Drupal\field\Entity\FieldConfig) {
      if ($definition->getSettings()['target_type'] == 'media') {
        $media = $node->{$definition->getName()}->entity;
        if ($media) {
          $media->groups = $node->groups;
          $media->save();
        }
      }
      if ($definition->getSettings()['target_type'] == 'paragraph') {
        $paragraph = $node->{$definition->getName()}->entity;
        if ($paragraph) {
          ph_content_update_media_items($paragraph, $node->groups);
        }
      }
    }
  }
}

/**
 * Set groups on all media items referenced in a paragraph.
 *
 * @param $paragraph
 *   The paragraph.
 * @param $groups
 *   The groups to set.
 */
function MY_MODULE_update_media_items($paragraph, $groups) {
  foreach ($paragraph->getFieldDefinitions() as $definition) {
    if ($definition instanceof \Drupal\field\Entity\FieldConfig) {
      if ($definition->getSettings()['target_type'] == 'media') {
        $media = $paragraph->{$definition->getName()}->entity;
        if ($media) {
          $media->groups = $groups;
          $media->save();
        }
      }
      if ($definition->getSettings()['target_type'] == 'paragraph') {
        $paragraph = $paragraph->{$definition->getName()}->entity;
        if ($paragraph) {
          ph_content_update_media_items($paragraph, $groups);
        }
      }
    }
  }
}
Source Link
user72672
user72672

With the help from @Clive I found a solution:

/**
 * Custom form submission for node form.
 */
function ph_content_node_form_submit(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $node = $form_state->getFormObject()->getEntity();

  foreach ($node->getFieldDefinitions() as $definition) {
    if ($definition instanceof \Drupal\field\Entity\FieldConfig) {
      if ($definition->getSettings()['target_type'] == 'media') {
        $media = $node->{$definition->getName()}->entity;
        if ($media) {
          $media->groups = $node->groups;
          $media->save();
        }
      }
      if ($definition->getSettings()['target_type'] == 'paragraph') {
        $paragraph = $node->{$definition->getName()}->entity;
        if ($paragraph) {
          ph_content_update_media_items($paragraph, $node->groups);
        }
      }
    }
  }
}

/**
 * Set groups on all media items referenced in a paragraph.
 *
 * @param $paragraph
 *   The paragraph.
 * @param $groups
 *   The groups to set.
 */
function ph_content_update_media_items($paragraph, $groups) {
  foreach ($paragraph->getFieldDefinitions() as $definition) {
    if ($definition instanceof \Drupal\field\Entity\FieldConfig) {
      if ($definition->getSettings()['target_type'] == 'media') {
        $media = $paragraph->{$definition->getName()}->entity;
        if ($media) {
          $media->groups = $groups;
          $media->save();
        }
      }
      if ($definition->getSettings()['target_type'] == 'paragraph') {
        $paragraph = $paragraph->{$definition->getName()}->entity;
        if ($paragraph) {
          ph_content_update_media_items($paragraph, $groups);
        }
      }
    }
  }
}