You are here

function farm_group_circular_membership_validate in farmOS 7

Form helper function for validating against circular membership assignment.

Parameters

array $asset_ids: An array of asset IDs that are being assigned to group(s).

array $group_ids: An array of group IDs that the assets are being assigned to.

string $element_name: The form element name to flag in form_set_error() if circular membership is detected.

3 calls to farm_group_circular_membership_validate()
farm_group_asset_form_validate in modules/farm/farm_group/farm_group.module
Validation handler for processing the asset group field.
farm_group_asset_membership_action_validate in modules/farm/farm_group/farm_group.module
Validation handler for farm_group_asset_membership action configuration form.
farm_group_field_farm_membership_validate in modules/farm/farm_group/farm_group.module
Validation callback for the field_farm_group field in logs.

File

modules/farm/farm_group/farm_group.module, line 857

Code

function farm_group_circular_membership_validate($asset_ids, $group_ids, $element_name) {

  // Iterate through the selected groups and assets to check for possible
  // circular membership.
  foreach ($group_ids as $group_id) {

    // If the group ID is empty, skip it.
    if (empty($group_id)) {
      continue;
    }

    // Load the group.
    $group = farm_asset_load($group_id);

    // If the group did not load, skip it.
    if (empty($group)) {
      continue;
    }

    // Iterate through the assets being assigned to the group.
    foreach ($asset_ids as $asset_id) {

      // If the asset ID is empty, skip it.
      if (empty($asset_id)) {
        continue;
      }

      // Load the asset.
      $asset = farm_asset_load($asset_id);

      // If the group did not load, skip it.
      if (empty($asset)) {
        continue;
      }

      // Check for a circular membership.
      $circular = farm_group_circular_membership($group, $asset);

      // If a circular membership is detected, warn the user.
      if ($circular) {

        // Get the URI information for the group and asset.
        $group_uri = entity_uri('farm_asset', $group);
        $asset_uri = entity_uri('farm_asset', $asset);

        // Create links to the asset and group.
        $group_link = l(entity_label('farm_asset', $group), $group_uri['path']);
        $asset_link = l(entity_label('farm_asset', $asset), $asset_uri['path']);

        // Set an error on the asset field and describe which asset and group
        // would create the circular membership.
        form_set_error($element_name, t('The asset "!asset_link" cannot be added to the group "!group_link" because it would create a circular membership.', array(
          '!asset_link' => $asset_link,
          '!group_link' => $group_link,
        )));
      }
    }
  }
}