entitytypemanager entity query conditions code example

Example: entitytypemanager entity query conditions

$query = \Drupal::entityQuery('entity_name');
    //field value condition check
    $query->condition('financial_status','accept');
    //field value array condition check
    $query->condition('financial_payment_status',['completed'],'IN');
    //condition with date format (d/m/y)
    if($params['start-date']){
      $befordate = $params['start-date'].' 23:59:59';
      $query->condition('changed',strtotime($befordate),'>=');
    }
    if($params['end-date']){
      $enddate = $params['end-date'].' 23:59:59';
      $query->condition('changed',strtotime($enddate),'<='); 
    }
    //search condition with name
    if($search)
       $query->condition('name', "%" . $search . "%", 'LIKE');
    //Or Conditon check with name or user name searching
    if($matter_search){
      $group = $query->orConditionGroup()
       ->condition('chat_matter_Id.entity.name', "%" . $matter_search . "%", 'LIKE')
       ->condition('chat_matter_Id.entity.user_id.entity.field_first_name', "%" . $matter_search . "%", 'LIKE');
      $query->condition($group);
    }
    //condition not equal a value
    $query->condition('financial_type','fees','!=');
    //today created items filter
    $query->condition('created', array(strtotime('today'), strtotime('today 23:59:59')), 'BETWEEN');
    //condition for item status is published
    $query->condition('status', 1);
    //sort list by created time
    $query->sort('created', 'DESC');
    //result range for pager
    if($offset && $limit){
     $start_limit = ($offset - 1) * $limit;
     $query->range($start_limit, $limit);
    }
    $entity_ids = $query->execute();
    if (!empty($entity_ids)) {
      $Entity = \Drupal::entityTypeManager()->getStorage('entity_name')->loadMultiple($entity_ids);
      foreach ($Entity as $node) {
        $data = [];
        $data['matter_id'] = $node->financial_matter_Id->entity->id->value;
        
        $payments[] = $data;
      }
    }

Tags:

Misc Example