drupal group get content type plugin
So recently writing some behat tests I was having some difficulty determing exactly how to formulate the plugin to use for creating group content.
Plugin formats are strings, that look like this: group_media:image
Seems simple enough. Just group_CONTENT_TYPE:BUNDLE.
Welp, I just hate using strings and string concatenation. It feels hokey, and I've had mixed results with it and content type plugins. So, if you want to make something automated work, here's a snippet of code I used with decent success:
// Find the correct plugin for the content being created.
$plugins = $group->getGroupType()->getInstalledContentPlugins();
foreach ($plugins as $plugin_id => $plugin) {
if (
(
$plugin->getEntityTypeId() == 'node'
|| $plugin->getEntityTypeId() == 'media'
)
&& $plugin->getEntityBundle() == $content_type
) {
$group->addContent($content, $plugin_id);
return;
}
}
Now of course your mileage may vary with this snippet, but if you're at this point you're likely a decent enough dev to abstract the above to suit your needs.
Is it more efficient than string concatenation? Nope, it's slower.
Is it less hokey because you aren't relying on strings and instead or using object methods? Yep.
Thank god there isn't time travel. Me that preferred procedural over object oriented would lambast this situation for clearly being inferior... oh how we all grow up!