src/ContextGenerator/JsonldContextGenerator.php
public function generateContext(RdfMappingInterface $rdfMapping) {
    // TODO: we will need to use \Drupal\Core\Field\FieldDefinitionInterface
    // a lot to be able to create/frame/discern drupal bundles based on JSON-LD
    // So keep an eye on that definition.
    $allRdfNameSpaces = rdf_get_namespaces();

    // This one will become our return value.
    $jsonLdContextArray['@context'] = [];

    // Temporary array to keep track of our used namespaces and props.
    $theAccumulator = [];

    $bundle_rdf_mappings = $rdfMapping->getPreparedBundleMapping();
    $drupal_types = $this->entityBundleIdsSplitter($rdfMapping->id());
    $entity_type_id = $drupal_types['entityTypeId'];
    $bundle = $drupal_types['bundleId'];
    // If we don't have rdf:type(s) for this bundle then it makes little
    // sense to continue.
    // This only generates an Exception if there is an
    // rdfmapping object but has no rdf:type.
    if (empty($bundle_rdf_mappings['types'])) {
      $msg = t("Can't generate JSON-LD Context without at least one rdf:type for Entity type @entity_type, Bundle @bundle_name combo.",
        ['@entity_type' => $entity_type_id, ' @bundle_name' => $bundle]);
      $this->logger->warning("@msg",
        [
          '@msg' => $msg,
        ]);
      throw new \Exception($msg);
    }

    /* We have a lot of assumptions here (rdf module is strange)
    a) xsd and other utility namespaces are in place
    b) the user knows what/how rdf mapping works and does it right
    c) that if a field's mapping_type is "rel" or "rev" and datatype is
    not defined, then '@type' is uncertain.
    d) that mapping back and forward is 1 to 1.
    Drupal allows multiple fields to be mapped to a same rdf prop
    but that does not scale back. If drupal gets an input with a list
    of values for a given property, we would never know in which Drupal
    fields we should put those values. it's the many to one,
    one to many reduction problem made worst by the abstraction of
    fields being containers of mappings and not rdf properties. */
    // Only care for those mappings that point to bundled or base fields.
    // First our bundled fields.
    foreach ($this->entityFieldManager->getFieldDefinitions($entity_type_id, $bundle) as $bundleFieldName => $fieldDefinition) {
      $field_context = $this->getFieldsRdf($rdfMapping, $bundleFieldName, $fieldDefinition, $allRdfNameSpaces);
      $theAccumulator = array_merge($field_context, $theAccumulator);
    }
    // And then our Base fields.
    foreach ($this->entityFieldManager->getBaseFieldDefinitions($entity_type_id) as $baseFieldName => $fieldDefinition) {
      $field_context = $this->getFieldsRdf($rdfMapping, $baseFieldName, $fieldDefinition, $allRdfNameSpaces);
      $theAccumulator = array_merge($field_context, $theAccumulator);
    }
    $theAccumulator = array_filter($theAccumulator);
    $jsonLdContextArray['@context'] = $theAccumulator;
    return json_encode($jsonLdContextArray, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
  }
public function generateContext(RdfMappingInterface $rdfMapping) {
    // TODO: we will need to use \Drupal\Core\Field\FieldDefinitionInterface
    // a lot to be able to create/frame/discern drupal bundles based on JSON-LD
    // So keep an eye on that definition.
    $allRdfNameSpaces = rdf_get_namespaces();

    // This one will become our return value.
    $jsonLdContextArray['@context'] = [];

    // Temporary array to keep track of our used namespaces and props.
    $theAccumulator = [];

    $bundle_rdf_mappings = $rdfMapping->getPreparedBundleMapping();
    $drupal_types = $this->entityBundleIdsSplitter($rdfMapping->id());
    $entity_type_id = $drupal_types['entityTypeId'];
    $bundle = $drupal_types['bundleId'];
    // If we don't have rdf:type(s) for this bundle then it makes little
    // sense to continue.
    // This only generates an Exception if there is an
    // rdfmapping object but has no rdf:type.
    if (empty($bundle_rdf_mappings['types'])) {
      $msg = t("Can't generate JSON-LD Context without at least one rdf:type for Entity type @entity_type, Bundle @bundle_name combo.",
        ['@entity_type' => $entity_type_id, ' @bundle_name' => $bundle]);
      $this->logger->warning("@msg",
        [
          '@msg' => $msg,
        ]);
      throw new \Exception($msg);
    }

    /* We have a lot of assumptions here (rdf module is strange)
    a) xsd and other utility namespaces are in place
    b) the user knows what/how rdf mapping works and does it right
    c) that if a field's mapping_type is "rel" or "rev" and datatype is
    not defined, then '@type' is uncertain.
    d) that mapping back and forward is 1 to 1.
    Drupal allows multiple fields to be mapped to a same rdf prop
    but that does not scale back. If drupal gets an input with a list
    of values for a given property, we would never know in which Drupal
    fields we should put those values. it's the many to one,
    one to many reduction problem made worst by the abstraction of
    fields being containers of mappings and not rdf properties. */
    // Only care for those mappings that point to bundled or base fields.
    // First our bundled fields.
    foreach ($this->entityFieldManager->getFieldDefinitions($entity_type_id, $bundle) as $bundleFieldName => $fieldDefinition) {
      $field_context = $this->getFieldsRdf($rdfMapping, $bundleFieldName, $fieldDefinition, $allRdfNameSpaces);
      $theAccumulator = array_merge($field_context, $theAccumulator);
    }
    // And then our Base fields.
    foreach ($this->entityFieldManager->getBaseFieldDefinitions($entity_type_id) as $baseFieldName => $fieldDefinition) {
      $field_context = $this->getFieldsRdf($rdfMapping, $baseFieldName, $fieldDefinition, $allRdfNameSpaces);
      $theAccumulator = array_merge($field_context, $theAccumulator);
    }
    $theAccumulator = array_filter($theAccumulator);
    $jsonLdContextArray['@context'] = $theAccumulator;
    return json_encode($jsonLdContextArray, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
  }
{@inheritdoc}