Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.0 kB
1
Indexable
Never
namespace Drupal\YOUR_MODULE_NAME\Plugin\views\field;

use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;

/**
 * A custom Views field that displays last name followed by first name.
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("custom_name_field")
 */
class CustomNameField extends FieldPluginBase {

  /**
   * Defines if this field is sortable.
   *
   * @var array
   */
  protected $click_sort = array('sql' => 'CONCAT(last_name_field, " ", first_name_field)');
  
  /**
   * {@inheritdoc}
   */
  public function clickSort($order) {
    $this->query->addOrderBy(NULL, NULL, $order, $this->click_sort);
  }

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
    $last_name = $this->getValue($values, 'last_name_field'); // Replace with your actual last name field name
    $first_name = $this->getValue($values, 'first_name_field'); // Replace with your actual first name field name
    return $last_name . ' ' . $first_name;
  }
}