You are here

function schemaorg_ui_field_ui_field_edit_form_submit in Schema.org 7

Submit function for edit field form.

1 string reference to 'schemaorg_ui_field_ui_field_edit_form_submit'
schemaorg_ui_form_field_ui_field_edit_form_alter in modules/schemaorg_ui/schemaorg_ui.module
Implements hook_form_FORM_ID_alter().

File

modules/schemaorg_ui/schemaorg_ui.module, line 96
User interface for setting the schema.org mappings

Code

function schemaorg_ui_field_ui_field_edit_form_submit($form, &$form_state) {
  $entity_type = $form['#instance']['entity_type'];
  $bundle = $form['#instance']['bundle'];
  $field_name = $form['#field']['field_name'];
  $field_type = $form['#field']['type'];
  $mapping = rdf_mapping_load($entity_type, $bundle);

  // This field might not have an RDF mapping yet.
  if (empty($mapping[$field_name])) {
    $mapping[$field_name]['predicates'] = array();
  }
  $mapping[$field_name]['predicates'] = schemaorg_ui_terms_merge($form_state['values']['schemaorg_ui_field_property'], $mapping[$field_name]['predicates']);

  // Sets RDF mapping type in the case of fields where the object is a resource
  // such as image, file, etc.
  $rel_field_types = array(
    'image',
    'file',
    'taxonomy_term_reference',
  );
  if (in_array($field_type, $rel_field_types)) {
    $mapping[$field_name]['type'] = 'rel';
  }

  // Performs some maintenance tasks based on whether the mapping contains
  // schema.org terms or not.
  // Scans the mapping array to see if some fields are mapped to schema.org.
  $schemaorg_mappings = FALSE;

  // Some fields are ignored since they are not input by the user.
  $ignored_fields = array(
    'title',
    'name',
    'url',
  );
  foreach ($mapping as $field => $info) {
    if (!empty($info['predicates']) && !in_array($field, $ignored_fields)) {
      if (count($info['predicates']) != count(array_filter($info['predicates'], 'schemaorg_ui_filter_schema_term'))) {
        $schemaorg_mappings = TRUE;
        break;
      }
    }
  }
  if ($schemaorg_mappings) {

    // Specifies the title/name mapping as expected by schema.org. This mapping
    // is always set to schema:name and is not exposed in the UI.
    // The label of an entity is usually either 'title' (e.g. node) or
    // 'name' (e.g. user).
    if (!empty($mapping['title'])) {
      $mapping['title']['predicates'] = array(
        'schema:name',
      );
    }
    if (!empty($mapping['name'])) {
      $mapping['name']['predicates'] = array(
        'schema:name',
      );
    }

    // Sets the mapping for the url of the entity. This mapping is always set
    // to schema:url and is not exposed in the UI.
    if ($entity_type != 'profile2') {
      $mapping['url']['predicates'] = array(
        'schema:url',
      );
      $mapping['url']['type'] = 'rel';
    }

    // Add schema:Person type to user mapping.
    if ($entity_type == 'user' && $bundle == 'user') {
      $mapping['rdftype'] = schemaorg_ui_terms_merge('Person', $mapping['rdftype']);
    }
  }
  else {

    // Makes sure no schema.org mapping for title/name remains if no schema.org
    // terms are used.
    if (!empty($mapping['title'])) {
      $mapping['title']['predicates'] = array_filter($mapping['title']['predicates'], 'schemaorg_ui_filter_schema_term');
    }
    if (!empty($mapping['name'])) {
      $mapping['name']['predicates'] = array_filter($mapping['name']['predicates'], 'schemaorg_ui_filter_schema_term');
    }

    // Since this pseudo-field mapping is only used for the purpose of
    // schema.org, it is entirely removed.
    unset($mapping['url']);

    // Remove schema.org type from the user mapping.
    if ($entity_type == 'user' && $bundle == 'user') {
      $mapping['rdftype'] = array_filter($mapping['rdftype'], 'schemaorg_ui_filter_schema_term');
    }
  }
  rdf_mapping_save(array(
    'type' => $entity_type,
    'bundle' => $bundle,
    'mapping' => $mapping,
  ));

  // Handle profile2 and schema.org mappings by setting the right type and
  // properties for the user entity type. The profile should not be seen as a
  // separate resource in the RDFa, but instead it should be part of the user.
  if (module_exists('profile2')) {
    $user_mapping = rdf_mapping_load('user', 'user');
    $user_mapping['rdftype'] = schemaorg_ui_terms_merge('Person', isset($user_mapping['rdftype']) ? $user_mapping['rdftype'] : array());
    $user_mapping['name']['predicates'] = array(
      'schema:name',
    );
    $user_mapping['url']['predicates'] = array(
      'schema:url',
    );
    $user_mapping['url']['type'] = 'rel';
    rdf_mapping_save(array(
      'type' => 'user',
      'bundle' => 'user',
      'mapping' => $user_mapping,
    ));
  }
}